using System;
using System.Collections;
using System.Data;
using PCSComProduct.Costing.DS;
using PCSComProduct.Items.DS;

using PCSComUtils.PCSExc;
using PCSComProduct.Items.BO;
using PCSComUtils.DataContext;

namespace PCSComProduct.Costing.BO
{
	public class MultiLevelCostRollUpBO// : IMultiLevelCostRollUpBO
	{
		private const string THIS = "PCSComProduct.Costing.BO.MultiLevelCostRollUpBO";
		private const int DECIMAL_ROUND_NUMBER = 5;
		public MultiLevelCostRollUpBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}


	
		public void Add(object pObjectDetail)
		{
			// TODO:  Add MultiLevelCostRoleUpBO.Add implementation

		}
	
		public void Delete(object pObjectVO)
		{
			// TODO:  Add MultiLevelCostRoleUpBO.Delete implementation

		}
	
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add MultiLevelCostRoleUpBO.GetObjectVO implementation
			return null;
		}
	
		public void Update(object pObjectDetail)
		{
			// TODO:  Add MultiLevelCostRoleUpBO.Update implementation

		}
	
		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  Add MultiLevelCostRoleUpBO.UpdateDataSet implementation

		}
		//**************************************************************************              
		///    <Description>
		///       This method uses to get ITM_ProductVO object
		///    </Description>
		///    <Inputs>
		///        int Id
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       ITM_ProductVO
		///    </Returns>
		///    <Authors>
		///       DungLa
		///    </Authors>
		///    <History>
		///       21-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public ArrayList GetAllComponents(int pintProductID)
		{
			try
			{
				ArrayList arrComponents = new ArrayList();
				ITM_BOMDS dsBOM = new ITM_BOMDS();
				arrComponents = dsBOM.GetComponentOfItem(pintProductID);
				arrComponents.TrimToSize();
				ArrayList arrItemComponent = new ArrayList();
				for (int i = 0; i < arrComponents.Count; i++)
				{
					ITM_BOMVO voBOM = (ITM_BOMVO)arrComponents[i];
					ITM_Product voProduct = new ProductItemInfoBO().GetProductInfo(voBOM.ComponentID);
					arrItemComponent.Add(voProduct);
				}

				arrItemComponent.TrimToSize();
				return arrComponents;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method uses to get ITM_ProductVO object
		///    </Description>
		///    <Inputs>
		///        int Id
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       ITM_ProductVO
		///    </Returns>
		///    <Authors>
		///       DungLa
		///    </Authors>
		///    <History>
		///       21-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public ArrayList GetAllItemBom(int pintProductID)
		{
			try
			{
				ArrayList arrComponents = new ArrayList();
				ITM_BOMDS dsBOM = new ITM_BOMDS();
				arrComponents = dsBOM.GetComponentOfItem(pintProductID);

				arrComponents.TrimToSize();
				return arrComponents;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		
		#region old code
		//**************************************************************************              
		///    <Description>
		///       This method uses to roll up cost of all sub product to specified product
		///    </Description>
		///    <Inputs>
		///        int Id, ArrayList AllBomItem, RollUp date
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       N/A
		///    </Returns>
		///    <Authors>
		///       DungLa
		///    </Authors>
		///    <History>
		///       21-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//*************************************************************************
		/*//[AutoComplete(true)]
		public void RollUpCost(int pintCCNID, DateTime pdtmRollUpDate, int pintProductID, ArrayList parrBOMItems)
		{
			try
			{
				// DS
				//ITM_BOMDS dsBOM = new ITM_BOMDS();
				ITM_CostDS dsCost = new ITM_CostDS();
				ITM_CostCenterRateDS dsCostCenterRate = new ITM_CostCenterRateDS();
				ITM_CostOperationDS dsCostOperation = new ITM_CostOperationDS();
				ITM_ProductDS dsProduct = new ITM_ProductDS();
				ITM_RoutingDS dsRouting = new ITM_RoutingDS();
				// VO
				ITM_ProductVO voProduct = new ITM_ProductVO();
				ITM_CostVO voCost = new ITM_CostVO();
				ITM_CostOperationVO voCostOperation = null;
				ITM_RoutingVO voRouting = new ITM_RoutingVO();
				ITM_CostCenterRateVO voCostCenterRate = new ITM_CostCenterRateVO();

				// list of all component which does not have routing id
				// we need to store it then roll up cost later
				ArrayList arrComponentNotInRouting = new ArrayList();

				// get current product info
				voProduct = (ITM_ProductVO)dsProduct.GetProductInfo(pintProductID);

				// get cost center rate of current product
				voCostCenterRate = (ITM_CostCenterRateVO)dsCostCenterRate.GetObjectVO(voProduct.CostCenterID, pdtmRollUpDate);
				// delete ITM_Cost buy ProductID, CCNID
				dsCost.Delete(pintCCNID, pintProductID);
				// delete ITM_CostOperation ProductID, CCNID
				dsCostOperation.Delete(pintCCNID, pintProductID);
				// get list routing of current product
				ArrayList arrRoutings = dsRouting.GetObjectVOs(pintProductID);
				// create new Cost, CostOperation object
				voCost.ProductID = pintProductID;
				voCost.CCNID = pintCCNID;
				voCost.SingleRollUpDate = pdtmRollUpDate;

				#region Rollup components cost 

				for (int j = 0; j < arrRoutings.Count; j++)
				{
					voRouting = (ITM_RoutingVO)arrRoutings[j];
					voCostOperation = new ITM_CostOperationVO();
					// general information
					voCostOperation.CCNID = pintCCNID;
					voCostOperation.ProductID = pintProductID;
					voCostOperation.RoutingID = voRouting.RoutingID;
					
					#region routing value added cost

					// 3. VaddCostMachineSetup03=((ITM_Routing.MachineSetupTime *ITM_CostCenterRate.MachineSetup)/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostMachineSetup03 = (voRouting.MachineSetupTime * voCostCenterRate.MachineSetup) / voRouting.SetupQuantity;
					}
					// 4. VaddCostMachineSetupFixed04 = (ITM_Routing.MachineSetupTime*ITM_CostCenterRate.MachineSetupFixedOverhead/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostMachineSetupFixed04 = (voRouting.MachineSetupTime * voCostCenterRate.MachineSetupFixedOverhead) / voRouting.SetupQuantity;
					}
					// 5. VaddCostMachineSetupVar05 = (VaddCostMachineSetup03 *ITM_CostCenterRate.MachineSetupVariableOverhead)
					voCostOperation.VAddCostMachineSetupVar05 = voCostOperation.VAddCostMachineSetup03 * voCostCenterRate.MachineSetupVariableOverhead;
					// 6. VaddCostMachineRun06=((ITM_Routing.MachineRunTime *ITM_CostCenterRate.MachineRun)/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostMachineRun06 = (voRouting.MachineRunTime * voCostCenterRate.MachineRun) / voRouting.RunQuantity;
					}
					// 7. VaddCostMachineFixed07 = (ITM_Routing.MachineRunTime*ITM_CostCenterRate.MachineRunFixedOverhead/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostMachineFixed07 = (voRouting.MachineRunTime * voCostCenterRate.MachineRunFixedOverhead) / voRouting.RunQuantity;
					}
					// 8. VaddCostMachineVariable08 = (VaddCostMachineRun06 *ITM_CostCenterRate.MachineRunVariableOverhead)
					voCostOperation.VAddCostMachineVariable08 = voCostOperation.VAddCostMachineRun06 * voCostCenterRate.MachineRunVariableOverhead;
					// 9. VaddCostLaborSetup09=((ITM_Routing.LaborSetupTime *ITM_CostCenterRate.laborSetup)/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostLaborSetup09 = (voRouting.LaborSetupTime * voCostCenterRate.LaborSetup) / voRouting.SetupQuantity;
					}
					// 10. VaddCostLaborSetupFixed10 = (ITM_Routing.LaborSetupTime*ITM_CostCenterRate.LaborSetupFixedOverhead/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostLaborSetupFixed10 = (voRouting.LaborSetupTime * voCostCenterRate.LaborSetupFixedOverhead) / voRouting.SetupQuantity;
					}
					// 11. VaddCostLaborSetupVar11 = (VaddCostLaborSetup09 *ITM_CostCenterRate.LaborSetupVariableOverhead)
					voCostOperation.VAddCostLaborSetupVariable11 = voCost.VAddCostLaborSetup09 * voCostCenterRate.LaborSetupVariableOverhead;
					// 12. VaddCostLaborRun12=((ITM_Routing.LaborRunTime *ITM_CostCenterRate.LaborRun)/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostLaborRun12 = (voRouting.LaborRunTime * voCostCenterRate.LaborRun) / voRouting.RunQuantity;
					}
					// 13. VaddCostLaborFixed13 = (ITM_Routing.LaborRunTime*ITM_CostCenterRate.LaborRunFixedOverhead/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostLaborFixed13 = (voRouting.LaborRunTime * voCostCenterRate.LaborSetupFixedOverhead) / voRouting.RunQuantity;
					}
					// 14. VaddCostLaborVariable14 = (VaddCostLaborRun12 *ITM_CostCenterRate.LaborRunVariableOverhead)
					voCostOperation.VAddCostLaborVariable14 = voCostOperation.VAddCostLaborRun12 * voCostCenterRate.LaborSetupVariableOverhead;
					// total
					voCostOperation.VAddCostTotalAmount21 = voCostOperation.VAddCostMaterial01 + voCostOperation.VAddCostMaterialOverHead02 
						+ voCostOperation.VAddCostMachineSetup03 + voCostOperation.VAddCostMachineSetupFixed04
						+ voCostOperation.VAddCostMachineSetupVar05 + voCostOperation.VAddCostMachineRun06
						+ voCostOperation.VAddCostMachineFixed07 + voCostOperation.VAddCostMachineVariable08
						+ voCostOperation.VAddCostLaborSetup09 + voCostOperation.VAddCostLaborSetupFixed10
						+ voCostOperation.VAddCostLaborSetupVariable11 + voCostOperation.VAddCostLaborRun12
						+ voCostOperation.VAddCostLaborFixed13 + voCostOperation.VAddCostLaborVariable14
						+ voCostOperation.VAddCostOutsideProc15 + voCostOperation.VAddCostAssemblyScrap16
						+ voCostOperation.VAddCostShrink17 + voCostOperation.VAddCostFreight18
						+ voCostOperation.VAddCostUserStandard1_19 + voCostOperation.VAddCostUserStandard2_20;

					#endregion

					#region routing components cost

					for (int i = 0; i < parrBOMItems.Count; i++)
					{
						// BOM item
						ITM_BOMVO voBOM = (ITM_BOMVO)(parrBOMItems[i]);
						// if current component is not in the list of routing 
						// then continue to next component
						if (voBOM.RoutingID != voRouting.RoutingID)
						{
							// if component not have routing then we add to list to roll up later
							if (voBOM.RoutingID = 0)
								arrComponentNotInRouting.Add(voBOM);
							continue;
						}
						// check if bom item have sub BOM or not
						ArrayList arrAllSubProducts = new ArrayList();
						arrAllSubProducts = GetAllComponents(voBOM.ComponentID);
						arrAllSubProducts.TrimToSize();
						// if component have sub component then recursive
						if (arrAllSubProducts.Count > 0)
						{
							RollUpCost(pintCCNID, pdtmRollUpDate, voBOM.ComponentID, arrAllSubProducts);
						}
						ITM_ProductVO voSubProduct = new ITM_ProductVO();
						ITM_CostVO voSubCost = new ITM_CostVO();
						// get sub product
						voSubProduct = (ITM_ProductVO) (dsProduct.GetProductInfo(voBOM.ComponentID));
						// sub product cost
						voSubCost = (ITM_CostVO) (dsCost.GetCostByProduct(voSubProduct.ProductID));
						
						// 1. ComMaterial01 = sum(sub  ComMaterial01)
						voCostOperation.ComMaterial01 += voSubCost.ItemCostMaterial01;
						// 2. ComMaterialOverHead02 = sum( sub  ComMaterial * MaterialOH from ITM_CostCenterRate) 
						voCostOperation.ComMaterialOverHead02 += voSubCost.ItemCostMaterial01 * voCostCenterRate.MaterialOverhead;
						// 3. ComMachineSetup03 =sum((ITM_Routing.Machine Setup Time * ITM_CostCenterRate.Machine Setup Rate) / ITM_Routing.SetupQuantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComMachineSetup03 += (voRouting.MachineSetupTime * voCostCenterRate.MachineSetup) / voRouting.SetupQuantity;
						}
						// 4. ComMachineSetupFixed04 = sum((ITM_Routing.Machine Setup Time * MachineSetupFixedOverhead ) / ITM_Routing.SetupQuantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComMachineSetupFixed04 += (voRouting.MachineSetupTime * voCostCenterRate.MachineSetupFixedOverhead) / voRouting.SetupQuantity;
						}
						// 5.ComMachineSetupVar05 = sum(sub ComMachineSetup03  *ITM_CostCenterRate. Machine Setup Var Overhead )
						voCostOperation.ComMachineSetupVar05 += voSubCost.ItemCostMachineSetup03 * voCostCenterRate.MachineSetupVariableOverhead;
						// 6.ComMachineRun06 =  sum((ITM_Routing.Machine Run Time * ITM_CostCenterRate.MachineRun) / ITM_Routing.RunQuantity)
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComMachineRun06 += (voRouting.MachineRunTime * voCostCenterRate.MachineRun) / voRouting.RunQuantity;
						}
						// 7.ComMachineFixed07 =sum(ITM_Routing.Machine Run Time * ITM_CostCenterRate.MachineRunFixedOverHead ) /  ITM_Routing.RunQuantity
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComMachineFixed07 += (voRouting.MachineRunTime * voCostCenterRate.MachineRunFixedOverhead) / voRouting.RunQuantity;
						}
						// 8. ComMachineVariable08 =  sum(sub ComMachineRun06*ITM_CostCenterRate. Machine Run Var Overhead )
						voCostOperation.ComMachineVariable08 += voSubCost.ItemCostMachineRun06 * voCostCenterRate.MachineRunVariableOverhead;
						// 9.ComLaborSetup09 =sum((ITM_Routing.Labor Setup Time * ITM_CostCenterRate.Labor Setup) / ITM_Routing.Setup Quantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComLaborSetup09 += (voRouting.LaborSetupTime * voCostCenterRate.LaborSetup) / voRouting.SetupQuantity;
						}
						// 10.ComLaborSetupFixed10 =sum((ITM_Routing.Labor Setup Time * ITM_CostCenterRate.LaborSetupFixedOverHead) / ITM_Routing.Setup Quantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComLaborSetupFixed10 += (voRouting.LaborSetupTime * voCostCenterRate.LaborSetupFixedOverhead) / voRouting.SetupQuantity;
						}
						// 11.ComLaborSetupVariable11= sum(sub ComLaborSetup09*ITM_CostCenterRate. Labor Setup Var Overhead)
						voCostOperation.ComLaborSetupVariable11 += voSubCost.ItemCostLaborSetup09 * voCostCenterRate.LaborSetupVariableOverhead;
						// 12.ComLaborRun12=  sum((ITM_Routing.Labor Run Time * ITM_CostCenterRate.LaborRun) / ITM_Routing.RunQuantity)
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComLaborRun12 += (voRouting.LaborRunTime * voCostCenterRate.LaborRun) / voRouting.RunQuantity;
						}
						// 13.ComLaborFixed13 =sum ((ITM_Routing.Labor Run Time * ITM_CostCenterRate.LaborRunFixedOverhead ) /  ITM_Routing.RunQuantity)
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComLaborFixed13 += (voRouting.LaborRunTime * voCostCenterRate.LaborRunFixedOverhead) / voRouting.RunQuantity;
						}
						// 14.ComLaborVariable14= sum(sub ComLaborRun12*ITM_CostCenterRate.LaborRunVarOverhead )
						voCostOperation.ComLaborVariable14 += voSubCost.ItemCostLaborRun12 * voCostCenterRate.LaborRunVariableOverhead;
						// 15.ComOutsideProc15=  Sum(sub ComOutsideProc15 * ITM_BOM.Quantity)
						voCostOperation.ComOutsideProc15 += voSubCost.ItemCostOutsideProc15 * voBOM.Quantity;
						// 16.ComAssemblyScrap16 =sum(sub ITM_Product.ScrapPercent * ITM_BOM.Quantity * ITM_Cost.ComTotalAmount)
						voCostOperation.ComAssemblyScrap16 += voSubProduct.ScrapPercent * voBOM.Quantity * voSubCost.ComTotalAmount21;
						// 17.ComShrink17 =sum(ITM_Bom.Shrink* ITM_BOM.Quantity*ITM_Cost.ComTotalAmount)
						voCostOperation.ComShrink17 += voBOM.Shrink * voBOM.Quantity * voSubCost.ComTotalAmount21;
						// 18.ComFreight18 = sum(sub ComFreight18 * ITM_BOM.Quantity)
						voCostOperation.ComFreight18 += voSubCost.ItemCostFreight18 * voBOM.Quantity;
						// 19.ComUserStandard1_19 = sum(sub ComUserStandard1_19 * ITM_BOM.Quantity)
						voCostOperation.ComUserStandard1_19 += voSubCost.ItemCostUserStandard1_19 * voBOM.Quantity;
						// 20.ComUserStandard2_20 = sum(sub ComUserStandard2_20 * ITM_BOM.Quantity)
						voCostOperation.ComUserStandard2_20 += voSubCost.ItemCostUserStandard2_20 * voBOM.Quantity;
						// 21.ComTotalAmount21 =sum (Com1 -> Com20)
						voCostOperation.ComTotalAmount21 += voCostOperation.ComMaterial01 + voCostOperation.ComMaterialOverHead02 
							+ voCostOperation.ComMachineSetup03 + voCostOperation.ComMachineSetupFixed04 + voCostOperation.ComMachineSetupVar05 
							+ voCostOperation.ComMachineRun06 + voCostOperation.ComMachineFixed07 + voCostOperation.ComMachineVariable08 
							+ voCostOperation.ComLaborSetup09 + voCostOperation.ComLaborSetupFixed10 + voCostOperation.ComLaborSetupVariable11 
							+ voCostOperation.ComLaborRun12 + voCostOperation.ComLaborFixed13 + voCostOperation.ComLaborVariable14 
							+ voCostOperation.ComOutsideProc15 + voCostOperation.ComAssemblyScrap16 + voCostOperation.ComShrink17 
							+ voCostOperation.ComFreight18 + voCostOperation.ComUserStandard1_19 + voCostOperation.ComUserStandard2_20;
					}

					#endregion

					#region routing operation cost

					voCostOperation.OperCostMaterial01 = voCostOperation.ComMaterial01 + voCostOperation.VAddCostMaterial01;
					voCostOperation.OperCostMaterialOverHead02 = voCostOperation.ComMaterialOverHead02 + voCostOperation.VAddCostMaterialOverHead02;
					voCostOperation.OperCostMaterialOverHead02 = voCostOperation.ComMaterialOverHead02 + voCostOperation.VAddCostMaterialOverHead02;
					voCostOperation.OperCostMachineSetup03 = voCostOperation.ComMachineSetup03 + voCostOperation.VAddCostMachineSetup03;
					voCostOperation.OperCostMachineSetupFixed04 = voCostOperation.ComMachineSetupFixed04 + voCostOperation.VAddCostMachineSetupFixed04;
					voCostOperation.OperCostMachineSetupVar05 = voCostOperation.ComMachineSetupVar05 + voCostOperation.VAddCostMachineSetupVar05;
					voCostOperation.OperCostMachineRun06 = voCostOperation.ComMachineRun06 + voCostOperation.VAddCostMachineRun06;
					voCostOperation.OperCostMachineFixed07 = voCostOperation.ComMachineFixed07 + voCostOperation.VAddCostMachineFixed07;
					voCostOperation.OperCostMachineVariable08 = voCostOperation.ComMachineVariable08 + voCostOperation.VAddCostMachineVariable08;
					voCostOperation.OperCostLaborSetup09 = voCostOperation.ComLaborSetup09 + voCostOperation.VAddCostLaborSetup09;
					voCostOperation.OperCostLaborSetupFixed10 = voCostOperation.ComLaborSetupFixed10 + voCostOperation.VAddCostLaborSetupFixed10;
					voCostOperation.OperCostLaborSetupVariable11 = voCostOperation.ComLaborSetupVariable11 + voCostOperation.VAddCostLaborSetupVariable11;
					voCostOperation.OperCostLaborRun12 = voCostOperation.ComLaborRun12 + voCostOperation.VAddCostLaborRun12;
					voCostOperation.OperCostLaborFixed13 = voCostOperation.ComLaborFixed13 + voCostOperation.VAddCostLaborFixed13;
					voCostOperation.OperCostLaborVariable14 = voCostOperation.ComLaborVariable14 + voCostOperation.VAddCostLaborVariable14;
					voCostOperation.OperCostOutsideProc15 = voCostOperation.ComOutsideProc15 + voCostOperation.VAddCostOutsideProc15;
					voCostOperation.OperCostAssemblyScrap16 = voCostOperation.ComAssemblyScrap16 + voCostOperation.VAddCostAssemblyScrap16;
					voCostOperation.OperCostShrink17 = voCostOperation.ComShrink17 + voCostOperation.VAddCostShrink17;
					voCostOperation.OperCostFreight18 = voCostOperation.ComFreight18 + voCostOperation.VAddCostFreight18;
					voCostOperation.OperCostUserStandard1_19 = voCostOperation.ComUserStandard1_19 + voCostOperation.VAddCostUserStandard1_19;
					voCostOperation.OperCostUserStandard2_20 = voCostOperation.ComUserStandard2_20 + voCostOperation.VAddCostUserStandard2_20;
					voCostOperation.OperCostTotalAmount21 = voCostOperation.ComTotalAmount21 + voCostOperation.VAddCostTotalAmount21;

					#endregion

					// save to database
					voCostOperation.CostOperationID = dsCostOperation.AddAndReturnID(voCostOperation);

					// now we calculate item cost base on operation cost
					#region item value added cost

					// 3. VaddCostMachineSetup03 = sum(ITM_CostOperation.VaddCostMachineSetup03)
					voCost.VAddCostMachineSetup03 += voCostOperation.VAddCostMachineSetup03;
					// 4. VaddCostMachineSetupFixed04 = sum(ITM_CostOperation.VaddCostMachineSetupFixed04)
					voCost.VAddCostMachineSetupFixed04 += voCostOperation.VAddCostMachineSetupFixed04;
					// 5. VaddCostMachineSetupVar05 = sum(ITM_CostOperation.VaddCostMachineSetupVar05)
					voCost.VAddCostMachineSetupVar05 += voCostOperation.VAddCostMachineSetupVar05;
					// 6. VaddCostMachineRun06 = sum(ITM_CostOperation.VaddCostMachineRun06)
					voCost.VAddCostMachineRun06 += voCostOperation.VAddCostMachineRun06;
					// 7. VaddCostMachineFixed07 = sum(ITM_CostOperation.VaddCostMachineFixed07)
					voCost.VAddCostMachineFixed07 += voCostOperation.VAddCostMachineFixed07;
					// 8. VaddCostMachineVariable08 = sum(ITM_CostOperation.VaddCostMachineVariable08)
					voCost.VAddCostMachineVariable08 += voCostOperation.VAddCostMachineVariable08;
					// 9. VaddCostLaborSetup09 = sum(ITM_CostOperation.VaddCostLaborSetup09)
					voCost.VAddCostLaborSetup09 += voCostOperation.VAddCostLaborSetup09;
					// 10. VaddCostLaborSetupFixed10 = sum(ITM_CostOperation.VaddCostLaborSetupFixed10)
					voCost.VAddCostLaborSetupFixed10 += (voCostOperation.VAddCostLaborSetupFixed10);
					// 11. VaddCostLaborSetupVar11 = sum(ITM_CostOperation.VaddCostLaborSetupVar11)
					voCost.VAddCostLaborSetupVariable11 += voCostOperation.VAddCostLaborSetupVariable11;
					// 12. VaddCostLaborRun12 = sum(ITM_CostOperation.VaddCostLaborRun12)
					voCost.VAddCostLaborRun12 += voCostOperation.VAddCostLaborRun12;
					// 13. VaddCostLaborFixed13 = sum(ITM_CostOperation.VaddCostLaborFixed13)
					voCost.VAddCostLaborFixed13 += voCostOperation.VAddCostLaborFixed13;
					// 14. VaddCostLaborVariable14 = sum(ITM_CostOperation.VaddCostLaborVariable14)
					voCost.VAddCostLaborVariable14 += voCostOperation.VAddCostLaborVariable14;
					// total
					voCost.VAddCostTotalAmount21 = voCost.VAddCostMaterial01 + voCost.VAddCostMaterialOverHead02 
						+ voCost.VAddCostMachineSetup03 + voCost.VAddCostMachineSetupFixed04
						+ voCost.VAddCostMachineSetupVar05 + voCost.VAddCostMachineRun06
						+ voCost.VAddCostMachineFixed07 + voCost.VAddCostMachineVariable08
						+ voCost.VAddCostLaborSetup09 + voCost.VAddCostLaborSetupFixed10
						+ voCost.VAddCostLaborSetupVariable11 + voCost.VAddCostLaborRun12
						+ voCost.VAddCostLaborFixed13 + voCost.VAddCostLaborVariable14
						+ voCost.VAddCostOutsideProc15 + voCost.VAddCostAssemblyScrap16
						+ voCost.VAddCostShrink17 + voCost.VAddCostFreight18
						+ voCost.VAddCostUserStandard1_19 + voCost.VAddCostUserStandard2_20;

					#endregion

					#region item components cost

					// 1. ComMaterial01 = sum(sub  ComMaterial01)
					voCost.ComMaterial01 += voCostOperation.ComMaterial01;
					// 2. ComMaterialOverHead02 = sum(ITM_CostOperation.ComMaterialOverHead02) 
					voCost.ComMaterialOverHead02 += voCostOperation.ComMaterialOverHead02;
					// 3. ComMachineSetup03 =sum(ITM_CostOperation.ComMachineSetup03)
					voCost.ComMachineSetup03 += voCostOperation.ComMachineSetup03;
					// 4. ComMachineSetupFixed04 = sum(ITM_CostOperation.ComMachineSetupFixed04)
					voCost.ComMachineSetupFixed04 += voCostOperation.ComMachineSetupFixed04;
					// 5.ComMachineSetupVar05 = sum(ITM_CostOperation.ComMachineSetupVar05)
					voCost.ComMachineSetupVar05 += voCostOperation.ComMachineSetupVar05;
					// 6.ComMachineRun06 =  sum(ITM_CostOperation.ComMachineRun06)
					voCost.ComMachineRun06 += voCostOperation.ComMachineRun06;
					// 7.ComMachineFixed07 =sum(ITM_CostOperation.ComMachineFixed07)
					voCost.ComMachineFixed07 += voCostOperation.ComMachineFixed07;
					// 8. ComMachineVariable08 =  sum(ITM_CostOperation.ComMachineVariable08)
					voCost.ComMachineVariable08 += voCostOperation.ComMachineVariable08;
					// 9.ComLaborSetup09 =sum(ITM_CostOperation.ComLaborSetup09)
					voCost.ComLaborSetup09 += voCostOperation.ComLaborSetup09;
					// 10.ComLaborSetupFixed10 =sum(ITM_CostOperation.ComLaborSetupFixed10)
					voCost.ComLaborSetupFixed10 += voCostOperation.ComLaborSetupFixed10;
					// 11.ComLaborSetupVariable11= sum(ITM_CostOperation.ComLaborSetupVariable11)
					voCost.ComLaborSetupVariable11 += voCostOperation.ComLaborSetupVariable11;
					// 12.ComLaborRun12=  sum(ITM_CostOperation.ComLaborRun12)
					voCost.ComLaborRun12 += voCostOperation.ComLaborRun12;
					// 13.ComLaborFixed13 =sum (ITM_CostOperation.ComLaborFixed13)
					voCost.ComLaborFixed13 += voCostOperation.ComLaborFixed13;
					// 14.ComLaborVariable14= sum(ITM_CostOperation.ComLaborVariable14)
					voCost.ComLaborVariable14 += voCostOperation.ComLaborVariable14;
					// 15.ComOutsideProc15=  Sum(ITM_CostOperation.ComOutsideProc15)
					voCost.ComOutsideProc15 += voCostOperation.ComOutsideProc15;
					// 16.ComAssemblyScrap16 =sum(ITM_CostOperation.ComAssemblyScrap16)
					voCost.ComAssemblyScrap16 += voCostOperation.ComAssemblyScrap16;
					// 17.ComShrink17 =sum(ITM_CostOperation.ComShrink17)
					voCost.ComShrink17 += voCostOperation.ComShrink17;
					// 18.ComFreight18 = sum(ITM_CostOperation.ComFreight18)
					voCost.ComFreight18 += voCostOperation.ComFreight18;
					// 19.ComUserStandard1_19 = sum(ITM_CostOperation.ComUserStandard1_19)
					voCost.ComUserStandard1_19 += voCostOperation.ComUserStandard1_19;
					// 20.ComUserStandard2_20 = sum(ITM_CostOperation.ComUserStandard2_20)
					voCost.ComUserStandard2_20 += voCostOperation.ComUserStandard2_20;
					// 21.ComTotalAmount21 =sum (Com1 -> Com20)
					voCost.ComTotalAmount21 += voCost.ComMaterial01 + voCost.ComMaterialOverHead02
						+ voCost.ComMachineSetup03 + voCost.ComMachineSetupFixed04
						+ voCost.ComMachineSetupVar05 + voCost.ComMachineRun06
						+ voCost.ComMachineFixed07 + voCost.ComMachineVariable08
						+ voCost.ComLaborSetup09 + voCost.ComLaborSetupFixed10
						+ voCost.ComLaborSetupVariable11 + voCost.ComLaborRun12
						+ voCost.ComLaborFixed13 + voCost.ComLaborVariable14
						+ voCost.ComOutsideProc15 + voCost.ComAssemblyScrap16
						+ voCost.ComShrink17 + voCost.ComFreight18
						+ voCost.ComUserStandard1_19 + voCost.ComUserStandard2_20;

					#endregion
				}

				#endregion

				#region Item cost

				// sum of materials cost and routing cost will be item cost

				voCost.ItemCostMaterial01 = voCost.ComMaterial01 + voCost.VAddCostMaterial01;
				voCost.ItemCostMaterialOverHead02 = voCost.ComMaterialOverHead02 + voCost.VAddCostMaterialOverHead02;
				voCost.ItemCostMachineSetup03 = voCost.ComMachineSetup03 + voCost.VAddCostMachineSetup03;
				voCost.ItemCostMachineSetupFixed04 = voCost.ComMachineSetupFixed04 + voCost.VAddCostMachineSetupFixed04;
				voCost.ItemCostMachineSetupVar05 = voCost.ComMachineSetupVar05 + voCost.VAddCostMachineSetupVar05;
				voCost.ItemCostMachineRun06 = voCost.ComMachineRun06 + voCost.VAddCostMachineRun06;
				voCost.ItemCostMachineFixed07 = voCost.ComMachineFixed07 + voCost.VAddCostMachineFixed07;
				voCost.ItemCostMachineVariable08 = voCost.ComMachineVariable08 + voCost.VAddCostMachineVariable08;
				voCost.ItemCostLaborSetup09 = voCost.ComLaborSetup09 + voCost.VAddCostLaborSetup09;
				voCost.ItemCostLaborSetupFixed10 = voCost.ComLaborSetupFixed10 + voCost.VAddCostLaborSetupFixed10;
				voCost.ItemCostLaborSetupVariable11 = voCost.ComLaborSetupVariable11 + voCost.VAddCostLaborSetupVariable11;
				voCost.ItemCostLaborRun12 = voCost.ComLaborRun12 + voCost.VAddCostLaborRun12;
				voCost.ItemCostLaborFixed13 = voCost.ComLaborFixed13 + voCost.VAddCostLaborFixed13;
				voCost.ItemCostLaborVariable14 = voCost.ComLaborVariable14 + voCost.VAddCostLaborVariable14;
				voCost.ItemCostOutsideProc15 = voCost.ComOutsideProc15 + voCost.VAddCostOutsideProc15;
				voCost.ItemCostAssemblyScrap16 = voCost.ComAssemblyScrap16 + voCost.VAddCostAssemblyScrap16;
				voCost.ItemCostShrink17 = voCost.ComShrink17 + voCost.VAddCostShrink17;
				voCost.ItemCostFreight18 = voCost.ComFreight18 + voCost.VAddCostFreight18;
				voCost.ItemCostUserStandard1_19 = voCost.ComUserStandard1_19 + voCost.VAddCostUserStandard1_19;
				voCost.ItemCostUserStandard2_20 = voCost.ComUserStandard2_20 + voCost.VAddCostUserStandard2_20;
				voCost.ItemCostTotalAmount21 = voCost.ComTotalAmount21 + voCost.VAddCostTotalAmount21;

				#endregion

				// save to database
				dsCost.Add(voCost);
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}*/

		#endregion
		//**************************************************************************              
		///    <Description>
		///       This method uses to roll up cost of all sub product to specified product
		///    </Description>
		///    <Inputs>
		///        int Id, ArrayList AllBomItem
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       N/A
		///    </Returns>
		///    <Authors>
		///       DungLa
		///    </Authors>
		///    <History>
		///       16-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void RollUpCostOld(int pintCCNID, DateTime pdtmRollUpDate, int pintProductID, ArrayList parrBOMItems)
		{
			try
			{
				// DS
				ITM_CostDS dsCost = new ITM_CostDS();
				ITM_CostCenterRateDS dsCostCenterRate = new ITM_CostCenterRateDS();
				ITM_CostOperationDS dsCostOperation = new ITM_CostOperationDS();
				ITM_ProductDS dsProduct = new ITM_ProductDS();
				ITM_RoutingDS dsRouting = new ITM_RoutingDS();
				// VO
				ITM_Product voProduct = new ITM_Product();
				ITM_CostVO voCost = new ITM_CostVO();
				ITM_CostOperationVO voCostOperation = null;
				ITM_RoutingVO voRouting = new ITM_RoutingVO();
				ITM_CostCenterRateVO voCostCenterRate = new ITM_CostCenterRateVO();

				// list of all component which does not have routing id
				// we need to store it then roll up cost later
				ArrayList arrComponentNotInRouting = new ArrayList();

				// get current product info
                voProduct = new ProductItemInfoBO().GetProductInfo(pintProductID);

				// get cost center rate of current product
                int iCostCenterId = voProduct.CostCenterID ?? 0;
                voCostCenterRate = (ITM_CostCenterRateVO)dsCostCenterRate.GetObjectVO(iCostCenterId, pdtmRollUpDate);
				// delete ITM_Cost buy ProductID, CCNID
				dsCost.Delete(pintCCNID, pintProductID);
				// delete ITM_CostOperation ProductID, CCNID
				dsCostOperation.Delete(pintCCNID, pintProductID);
				// get list routing of current product
				ArrayList arrRoutings = dsRouting.GetObjectVOs(pintProductID);
				arrRoutings.TrimToSize();
				// create new Cost, CostOperation object
				voCost.ProductID = pintProductID;
				voCost.CCNID = pintCCNID;
				voCost.SingleRollUpDate = pdtmRollUpDate;

				#region Rollup components cost 

				for (int j = 0; j < arrRoutings.Count; j++)
				{
					voRouting = (ITM_RoutingVO)arrRoutings[j];
					voCostOperation = new ITM_CostOperationVO();
					// general information
					voCostOperation.CCNID = pintCCNID;
					voCostOperation.ProductID = pintProductID;
					voCostOperation.RoutingID = voRouting.RoutingID;
					
					#region routing value added cost

					// 3. VaddCostMachineSetup03=((ITM_Routing.MachineSetupTime *ITM_CostCenterRate.MachineSetup)/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostMachineSetup03 = (voRouting.MachineSetupTime * voCostCenterRate.MachineSetup) / voRouting.SetupQuantity;
					}
					// 4. VaddCostMachineSetupFixed04 = (ITM_Routing.MachineSetupTime*ITM_CostCenterRate.MachineSetupFixedOverhead/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostMachineSetupFixed04 = (voRouting.MachineSetupTime * voCostCenterRate.MachineSetupFixedOverhead) / voRouting.SetupQuantity;
					}
					// 5. VaddCostMachineSetupVar05 = (VaddCostMachineSetup03 *ITM_CostCenterRate.MachineSetupVariableOverhead)
					voCostOperation.VAddCostMachineSetupVar05 = voCostOperation.VAddCostMachineSetup03 * voCostCenterRate.MachineSetupVariableOverhead;
					// 6. VaddCostMachineRun06=((ITM_Routing.MachineRunTime *ITM_CostCenterRate.MachineRun)/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostMachineRun06 = (voRouting.MachineRunTime * voCostCenterRate.MachineRun) / voRouting.RunQuantity;
					}
					// 7. VaddCostMachineFixed07 = (ITM_Routing.MachineRunTime*ITM_CostCenterRate.MachineRunFixedOverhead/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostMachineFixed07 = (voRouting.MachineRunTime * voCostCenterRate.MachineRunFixedOverhead) / voRouting.RunQuantity;
					}
					// 8. VaddCostMachineVariable08 = (VaddCostMachineRun06 *ITM_CostCenterRate.MachineRunVariableOverhead)
					voCostOperation.VAddCostMachineVariable08 = voCostOperation.VAddCostMachineRun06 * voCostCenterRate.MachineRunVariableOverhead;
					// 9. VaddCostLaborSetup09=((ITM_Routing.LaborSetupTime *ITM_CostCenterRate.laborSetup)/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostLaborSetup09 = (voRouting.LaborSetupTime * voCostCenterRate.LaborSetup) / voRouting.SetupQuantity;
					}
					// 10. VaddCostLaborSetupFixed10 = (ITM_Routing.LaborSetupTime*ITM_CostCenterRate.LaborSetupFixedOverhead/ITM_Routing.SetupQuantity)
					if (voRouting.SetupQuantity > 0)
					{
						voCostOperation.VAddCostLaborSetupFixed10 = (voRouting.LaborSetupTime * voCostCenterRate.LaborSetupFixedOverhead) / voRouting.SetupQuantity;
					}
					// 11. VaddCostLaborSetupVar11 = (VaddCostLaborSetup09 *ITM_CostCenterRate.LaborSetupVariableOverhead)
					voCostOperation.VAddCostLaborSetupVariable11 = voCost.VAddCostLaborSetup09 * voCostCenterRate.LaborSetupVariableOverhead;
					// 12. VaddCostLaborRun12=((ITM_Routing.LaborRunTime *ITM_CostCenterRate.LaborRun)/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostLaborRun12 = (voRouting.LaborRunTime * voCostCenterRate.LaborRun) / voRouting.RunQuantity;
					}
					// 13. VaddCostLaborFixed13 = (ITM_Routing.LaborRunTime*ITM_CostCenterRate.LaborRunFixedOverhead/ITM_Routing.RunQuantity)
					if (voRouting.RunQuantity > 0)
					{
						voCostOperation.VAddCostLaborFixed13 = (voRouting.LaborRunTime * voCostCenterRate.LaborSetupFixedOverhead) / voRouting.RunQuantity;
					}
					// 14. VaddCostLaborVariable14 = (VaddCostLaborRun12 *ITM_CostCenterRate.LaborRunVariableOverhead)
					voCostOperation.VAddCostLaborVariable14 = voCostOperation.VAddCostLaborRun12 * voCostCenterRate.LaborSetupVariableOverhead;
					// total
					voCostOperation.VAddCostTotalAmount21 = voCostOperation.VAddCostMaterial01 + voCostOperation.VAddCostMaterialOverHead02 
						+ voCostOperation.VAddCostMachineSetup03 + voCostOperation.VAddCostMachineSetupFixed04
						+ voCostOperation.VAddCostMachineSetupVar05 + voCostOperation.VAddCostMachineRun06
						+ voCostOperation.VAddCostMachineFixed07 + voCostOperation.VAddCostMachineVariable08
						+ voCostOperation.VAddCostLaborSetup09 + voCostOperation.VAddCostLaborSetupFixed10
						+ voCostOperation.VAddCostLaborSetupVariable11 + voCostOperation.VAddCostLaborRun12
						+ voCostOperation.VAddCostLaborFixed13 + voCostOperation.VAddCostLaborVariable14
						+ voCostOperation.VAddCostOutsideProc15 + voCostOperation.VAddCostAssemblyScrap16
						+ voCostOperation.VAddCostShrink17 + voCostOperation.VAddCostFreight18
						+ voCostOperation.VAddCostUserStandard1_19 + voCostOperation.VAddCostUserStandard2_20;

					#endregion

					#region routing components cost

					for (int i = 0; i < parrBOMItems.Count; i++)
					{
						// BOM item
						ITM_BOMVO voBOM = (ITM_BOMVO)(parrBOMItems[i]);
						// check if bom item have sub BOM or not
						ArrayList arrAllSubProducts = new ArrayList();
						arrAllSubProducts = GetAllItemBom(voBOM.ComponentID);
						arrAllSubProducts.TrimToSize();
						// if component have sub component then recursive
						if (arrAllSubProducts.Count > 0)
						{
							RollUpCost(pintCCNID, pdtmRollUpDate, voBOM.ComponentID, arrAllSubProducts);
						}
						// if current component is not in the list of routing 
						// then continue to next component
						if (voBOM.RoutingID != voRouting.RoutingID)
						{
							// if component not have routing then we add to list to roll up later
							if (voBOM.RoutingID == 0)
								arrComponentNotInRouting.Add(voBOM);
							continue;
						}
						ITM_Product voSubProduct = new ITM_Product();
						ITM_CostVO voSubCost = new ITM_CostVO();
						// get sub product info
						voSubProduct = new ProductItemInfoBO().GetProductInfo(voBOM.ComponentID);
						// sub product cost
						voSubCost = (ITM_CostVO) (dsCost.GetCostByProduct(voSubProduct.ProductID));
						
						// 1. ComMaterial01 = sum(sub  ComMaterial01)
						voCostOperation.ComMaterial01 += voSubCost.ItemCostMaterial01;
						// 2. ComMaterialOverHead02 = sum( sub  ComMaterial * MaterialOH from ITM_CostCenterRate) 
						voCostOperation.ComMaterialOverHead02 += voSubCost.ItemCostMaterial01 * voCostCenterRate.MaterialOverhead;
						// 3. ComMachineSetup03 =sum((ITM_Routing.Machine Setup Time * ITM_CostCenterRate.Machine Setup Rate) / ITM_Routing.SetupQuantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComMachineSetup03 += (voRouting.MachineSetupTime * voCostCenterRate.MachineSetup) / voRouting.SetupQuantity;
						}
						// 4. ComMachineSetupFixed04 = sum((ITM_Routing.Machine Setup Time * MachineSetupFixedOverhead ) / ITM_Routing.SetupQuantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComMachineSetupFixed04 += (voRouting.MachineSetupTime * voCostCenterRate.MachineSetupFixedOverhead) / voRouting.SetupQuantity;
						}
						// 5.ComMachineSetupVar05 = sum(sub ComMachineSetup03  *ITM_CostCenterRate. Machine Setup Var Overhead )
						voCostOperation.ComMachineSetupVar05 += voSubCost.ItemCostMachineSetup03 * voCostCenterRate.MachineSetupVariableOverhead;
						// 6.ComMachineRun06 =  sum((ITM_Routing.Machine Run Time * ITM_CostCenterRate.MachineRun) / ITM_Routing.RunQuantity)
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComMachineRun06 += (voRouting.MachineRunTime * voCostCenterRate.MachineRun) / voRouting.RunQuantity;
						}
						// 7.ComMachineFixed07 =sum(ITM_Routing.Machine Run Time * ITM_CostCenterRate.MachineRunFixedOverHead ) /  ITM_Routing.RunQuantity
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComMachineFixed07 += (voRouting.MachineRunTime * voCostCenterRate.MachineRunFixedOverhead) / voRouting.RunQuantity;
						}
						// 8. ComMachineVariable08 =  sum(sub ComMachineRun06*ITM_CostCenterRate. Machine Run Var Overhead )
						voCostOperation.ComMachineVariable08 += voSubCost.ItemCostMachineRun06 * voCostCenterRate.MachineRunVariableOverhead;
						// 9.ComLaborSetup09 =sum((ITM_Routing.Labor Setup Time * ITM_CostCenterRate.Labor Setup) / ITM_Routing.Setup Quantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComLaborSetup09 += (voRouting.LaborSetupTime * voCostCenterRate.LaborSetup) / voRouting.SetupQuantity;
						}
						// 10.ComLaborSetupFixed10 =sum((ITM_Routing.Labor Setup Time * ITM_CostCenterRate.LaborSetupFixedOverHead) / ITM_Routing.Setup Quantity)
						if (voRouting.SetupQuantity > 0)
						{
							voCostOperation.ComLaborSetupFixed10 += (voRouting.LaborSetupTime * voCostCenterRate.LaborSetupFixedOverhead) / voRouting.SetupQuantity;
						}
						// 11.ComLaborSetupVariable11= sum(sub ComLaborSetup09*ITM_CostCenterRate. Labor Setup Var Overhead)
						voCostOperation.ComLaborSetupVariable11 += voSubCost.ItemCostLaborSetup09 * voCostCenterRate.LaborSetupVariableOverhead;
						// 12.ComLaborRun12=  sum((ITM_Routing.Labor Run Time * ITM_CostCenterRate.LaborRun) / ITM_Routing.RunQuantity)
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComLaborRun12 += (voRouting.LaborRunTime * voCostCenterRate.LaborRun) / voRouting.RunQuantity;
						}
						// 13.ComLaborFixed13 =sum ((ITM_Routing.Labor Run Time * ITM_CostCenterRate.LaborRunFixedOverhead ) /  ITM_Routing.RunQuantity)
						if (voRouting.RunQuantity > 0)
						{
							voCostOperation.ComLaborFixed13 += (voRouting.LaborRunTime * voCostCenterRate.LaborRunFixedOverhead) / voRouting.RunQuantity;
						}
						// 14.ComLaborVariable14= sum(sub ComLaborRun12*ITM_CostCenterRate.LaborRunVarOverhead )
						voCostOperation.ComLaborVariable14 += voSubCost.ItemCostLaborRun12 * voCostCenterRate.LaborRunVariableOverhead;
						// 15.ComOutsideProc15=  Sum(sub ComOutsideProc15 * ITM_BOM.Quantity)
						voCostOperation.ComOutsideProc15 += voSubCost.ItemCostOutsideProc15 * voBOM.Quantity;
						// 16.ComAssemblyScrap16 =sum(sub ITM_Product.ScrapPercent * ITM_BOM.Quantity * ITM_Cost.ComTotalAmount)
                        double dScrapPercent = voSubProduct.ScrapPercent ?? 0;
                        voCostOperation.ComAssemblyScrap16 += Convert.ToDecimal(dScrapPercent) * voBOM.Quantity * voSubCost.ComTotalAmount21;
						// 17.ComShrink17 =sum(ITM_Bom.Shrink* ITM_BOM.Quantity*ITM_Cost.ComTotalAmount)
						voCostOperation.ComShrink17 += voBOM.Shrink * voBOM.Quantity * voSubCost.ComTotalAmount21;
						// 18.ComFreight18 = sum(sub ComFreight18 * ITM_BOM.Quantity)
						voCostOperation.ComFreight18 += voSubCost.ItemCostFreight18 * voBOM.Quantity;
						// 19.ComUserStandard1_19 = sum(sub ComUserStandard1_19 * ITM_BOM.Quantity)
						voCostOperation.ComUserStandard1_19 += voSubCost.ItemCostUserStandard1_19 * voBOM.Quantity;
						// 20.ComUserStandard2_20 = sum(sub ComUserStandard2_20 * ITM_BOM.Quantity)
						voCostOperation.ComUserStandard2_20 += voSubCost.ItemCostUserStandard2_20 * voBOM.Quantity;
						// 21.ComTotalAmount21 =sum (Com1 -> Com20)
						voCostOperation.ComTotalAmount21 += voCostOperation.ComMaterial01 + voCostOperation.ComMaterialOverHead02 
							+ voCostOperation.ComMachineSetup03 + voCostOperation.ComMachineSetupFixed04 + voCostOperation.ComMachineSetupVar05 
							+ voCostOperation.ComMachineRun06 + voCostOperation.ComMachineFixed07 + voCostOperation.ComMachineVariable08 
							+ voCostOperation.ComLaborSetup09 + voCostOperation.ComLaborSetupFixed10 + voCostOperation.ComLaborSetupVariable11 
							+ voCostOperation.ComLaborRun12 + voCostOperation.ComLaborFixed13 + voCostOperation.ComLaborVariable14 
							+ voCostOperation.ComOutsideProc15 + voCostOperation.ComAssemblyScrap16 + voCostOperation.ComShrink17 
							+ voCostOperation.ComFreight18 + voCostOperation.ComUserStandard1_19 + voCostOperation.ComUserStandard2_20;
					}

					#endregion

					#region routing operation cost

					voCostOperation.OperCostMaterial01 = voCostOperation.ComMaterial01 + voCostOperation.VAddCostMaterial01;
					voCostOperation.OperCostMaterialOverHead02 = voCostOperation.ComMaterialOverHead02 + voCostOperation.VAddCostMaterialOverHead02;
					voCostOperation.OperCostMaterialOverHead02 = voCostOperation.ComMaterialOverHead02 + voCostOperation.VAddCostMaterialOverHead02;
					voCostOperation.OperCostMachineSetup03 = voCostOperation.ComMachineSetup03 + voCostOperation.VAddCostMachineSetup03;
					voCostOperation.OperCostMachineSetupFixed04 = voCostOperation.ComMachineSetupFixed04 + voCostOperation.VAddCostMachineSetupFixed04;
					voCostOperation.OperCostMachineSetupVar05 = voCostOperation.ComMachineSetupVar05 + voCostOperation.VAddCostMachineSetupVar05;
					voCostOperation.OperCostMachineRun06 = voCostOperation.ComMachineRun06 + voCostOperation.VAddCostMachineRun06;
					voCostOperation.OperCostMachineFixed07 = voCostOperation.ComMachineFixed07 + voCostOperation.VAddCostMachineFixed07;
					voCostOperation.OperCostMachineVariable08 = voCostOperation.ComMachineVariable08 + voCostOperation.VAddCostMachineVariable08;
					voCostOperation.OperCostLaborSetup09 = voCostOperation.ComLaborSetup09 + voCostOperation.VAddCostLaborSetup09;
					voCostOperation.OperCostLaborSetupFixed10 = voCostOperation.ComLaborSetupFixed10 + voCostOperation.VAddCostLaborSetupFixed10;
					voCostOperation.OperCostLaborSetupVariable11 = voCostOperation.ComLaborSetupVariable11 + voCostOperation.VAddCostLaborSetupVariable11;
					voCostOperation.OperCostLaborRun12 = voCostOperation.ComLaborRun12 + voCostOperation.VAddCostLaborRun12;
					voCostOperation.OperCostLaborFixed13 = voCostOperation.ComLaborFixed13 + voCostOperation.VAddCostLaborFixed13;
					voCostOperation.OperCostLaborVariable14 = voCostOperation.ComLaborVariable14 + voCostOperation.VAddCostLaborVariable14;
					voCostOperation.OperCostOutsideProc15 = voCostOperation.ComOutsideProc15 + voCostOperation.VAddCostOutsideProc15;
					voCostOperation.OperCostAssemblyScrap16 = voCostOperation.ComAssemblyScrap16 + voCostOperation.VAddCostAssemblyScrap16;
					voCostOperation.OperCostShrink17 = voCostOperation.ComShrink17 + voCostOperation.VAddCostShrink17;
					voCostOperation.OperCostFreight18 = voCostOperation.ComFreight18 + voCostOperation.VAddCostFreight18;
					voCostOperation.OperCostUserStandard1_19 = voCostOperation.ComUserStandard1_19 + voCostOperation.VAddCostUserStandard1_19;
					voCostOperation.OperCostUserStandard2_20 = voCostOperation.ComUserStandard2_20 + voCostOperation.VAddCostUserStandard2_20;
					voCostOperation.OperCostTotalAmount21 = voCostOperation.ComTotalAmount21 + voCostOperation.VAddCostTotalAmount21;

					#endregion

					// save to database
					voCostOperation.CostOperationID = dsCostOperation.AddAndReturnID(voCostOperation);

					// now we calculate item cost base on operation cost
					#region item value added cost

					// 3. VaddCostMachineSetup03 = sum(ITM_CostOperation.VaddCostMachineSetup03)
					voCost.VAddCostMachineSetup03 += voCostOperation.VAddCostMachineSetup03;
					// 4. VaddCostMachineSetupFixed04 = sum(ITM_CostOperation.VaddCostMachineSetupFixed04)
					voCost.VAddCostMachineSetupFixed04 += voCostOperation.VAddCostMachineSetupFixed04;
					// 5. VaddCostMachineSetupVar05 = sum(ITM_CostOperation.VaddCostMachineSetupVar05)
					voCost.VAddCostMachineSetupVar05 += voCostOperation.VAddCostMachineSetupVar05;
					// 6. VaddCostMachineRun06 = sum(ITM_CostOperation.VaddCostMachineRun06)
					voCost.VAddCostMachineRun06 += voCostOperation.VAddCostMachineRun06;
					// 7. VaddCostMachineFixed07 = sum(ITM_CostOperation.VaddCostMachineFixed07)
					voCost.VAddCostMachineFixed07 += voCostOperation.VAddCostMachineFixed07;
					// 8. VaddCostMachineVariable08 = sum(ITM_CostOperation.VaddCostMachineVariable08)
					voCost.VAddCostMachineVariable08 += voCostOperation.VAddCostMachineVariable08;
					// 9. VaddCostLaborSetup09 = sum(ITM_CostOperation.VaddCostLaborSetup09)
					voCost.VAddCostLaborSetup09 += voCostOperation.VAddCostLaborSetup09;
					// 10. VaddCostLaborSetupFixed10 = sum(ITM_CostOperation.VaddCostLaborSetupFixed10)
					voCost.VAddCostLaborSetupFixed10 += (voCostOperation.VAddCostLaborSetupFixed10);
					// 11. VaddCostLaborSetupVar11 = sum(ITM_CostOperation.VaddCostLaborSetupVar11)
					voCost.VAddCostLaborSetupVariable11 += voCostOperation.VAddCostLaborSetupVariable11;
					// 12. VaddCostLaborRun12 = sum(ITM_CostOperation.VaddCostLaborRun12)
					voCost.VAddCostLaborRun12 += voCostOperation.VAddCostLaborRun12;
					// 13. VaddCostLaborFixed13 = sum(ITM_CostOperation.VaddCostLaborFixed13)
					voCost.VAddCostLaborFixed13 += voCostOperation.VAddCostLaborFixed13;
					// 14. VaddCostLaborVariable14 = sum(ITM_CostOperation.VaddCostLaborVariable14)
					voCost.VAddCostLaborVariable14 += voCostOperation.VAddCostLaborVariable14;
					// total
					voCost.VAddCostTotalAmount21 = voCost.VAddCostMaterial01 + voCost.VAddCostMaterialOverHead02 
						+ voCost.VAddCostMachineSetup03 + voCost.VAddCostMachineSetupFixed04
						+ voCost.VAddCostMachineSetupVar05 + voCost.VAddCostMachineRun06
						+ voCost.VAddCostMachineFixed07 + voCost.VAddCostMachineVariable08
						+ voCost.VAddCostLaborSetup09 + voCost.VAddCostLaborSetupFixed10
						+ voCost.VAddCostLaborSetupVariable11 + voCost.VAddCostLaborRun12
						+ voCost.VAddCostLaborFixed13 + voCost.VAddCostLaborVariable14
						+ voCost.VAddCostOutsideProc15 + voCost.VAddCostAssemblyScrap16
						+ voCost.VAddCostShrink17 + voCost.VAddCostFreight18
						+ voCost.VAddCostUserStandard1_19 + voCost.VAddCostUserStandard2_20;

					#endregion

					#region item components cost

					// 1. ComMaterial01 = sum(sub  ComMaterial01)
					voCost.ComMaterial01 += voCostOperation.ComMaterial01;
					// 2. ComMaterialOverHead02 = sum(ITM_CostOperation.ComMaterialOverHead02) 
					voCost.ComMaterialOverHead02 += voCostOperation.ComMaterialOverHead02;
					// 3. ComMachineSetup03 =sum(ITM_CostOperation.ComMachineSetup03)
					voCost.ComMachineSetup03 += voCostOperation.ComMachineSetup03;
					// 4. ComMachineSetupFixed04 = sum(ITM_CostOperation.ComMachineSetupFixed04)
					voCost.ComMachineSetupFixed04 += voCostOperation.ComMachineSetupFixed04;
					// 5.ComMachineSetupVar05 = sum(ITM_CostOperation.ComMachineSetupVar05)
					voCost.ComMachineSetupVar05 += voCostOperation.ComMachineSetupVar05;
					// 6.ComMachineRun06 =  sum(ITM_CostOperation.ComMachineRun06)
					voCost.ComMachineRun06 += voCostOperation.ComMachineRun06;
					// 7.ComMachineFixed07 =sum(ITM_CostOperation.ComMachineFixed07)
					voCost.ComMachineFixed07 += voCostOperation.ComMachineFixed07;
					// 8. ComMachineVariable08 =  sum(ITM_CostOperation.ComMachineVariable08)
					voCost.ComMachineVariable08 += voCostOperation.ComMachineVariable08;
					// 9.ComLaborSetup09 =sum(ITM_CostOperation.ComLaborSetup09)
					voCost.ComLaborSetup09 += voCostOperation.ComLaborSetup09;
					// 10.ComLaborSetupFixed10 =sum(ITM_CostOperation.ComLaborSetupFixed10)
					voCost.ComLaborSetupFixed10 += voCostOperation.ComLaborSetupFixed10;
					// 11.ComLaborSetupVariable11= sum(ITM_CostOperation.ComLaborSetupVariable11)
					voCost.ComLaborSetupVariable11 += voCostOperation.ComLaborSetupVariable11;
					// 12.ComLaborRun12=  sum(ITM_CostOperation.ComLaborRun12)
					voCost.ComLaborRun12 += voCostOperation.ComLaborRun12;
					// 13.ComLaborFixed13 =sum (ITM_CostOperation.ComLaborFixed13)
					voCost.ComLaborFixed13 += voCostOperation.ComLaborFixed13;
					// 14.ComLaborVariable14= sum(ITM_CostOperation.ComLaborVariable14)
					voCost.ComLaborVariable14 += voCostOperation.ComLaborVariable14;
					// 15.ComOutsideProc15=  Sum(ITM_CostOperation.ComOutsideProc15)
					voCost.ComOutsideProc15 += voCostOperation.ComOutsideProc15;
					// 16.ComAssemblyScrap16 =sum(ITM_CostOperation.ComAssemblyScrap16)
					voCost.ComAssemblyScrap16 += voCostOperation.ComAssemblyScrap16;
					// 17.ComShrink17 =sum(ITM_CostOperation.ComShrink17)
					voCost.ComShrink17 += voCostOperation.ComShrink17;
					// 18.ComFreight18 = sum(ITM_CostOperation.ComFreight18)
					voCost.ComFreight18 += voCostOperation.ComFreight18;
					// 19.ComUserStandard1_19 = sum(ITM_CostOperation.ComUserStandard1_19)
					voCost.ComUserStandard1_19 += voCostOperation.ComUserStandard1_19;
					// 20.ComUserStandard2_20 = sum(ITM_CostOperation.ComUserStandard2_20)
					voCost.ComUserStandard2_20 += voCostOperation.ComUserStandard2_20;
					// 21.ComTotalAmount21 =sum (Com1 -> Com20)
					voCost.ComTotalAmount21 += voCost.ComMaterial01 + voCost.ComMaterialOverHead02
						+ voCost.ComMachineSetup03 + voCost.ComMachineSetupFixed04
						+ voCost.ComMachineSetupVar05 + voCost.ComMachineRun06
						+ voCost.ComMachineFixed07 + voCost.ComMachineVariable08
						+ voCost.ComLaborSetup09 + voCost.ComLaborSetupFixed10
						+ voCost.ComLaborSetupVariable11 + voCost.ComLaborRun12
						+ voCost.ComLaborFixed13 + voCost.ComLaborVariable14
						+ voCost.ComOutsideProc15 + voCost.ComAssemblyScrap16
						+ voCost.ComShrink17 + voCost.ComFreight18
						+ voCost.ComUserStandard1_19 + voCost.ComUserStandard2_20;

					#endregion
				}

				#endregion

				#region item not have routing

				arrComponentNotInRouting.TrimToSize();
				// for all item that does not have routing, roll up will its childs' cost
				for (int i = 0; i < arrComponentNotInRouting.Count; i++)
				{
					// BOM item
					ITM_BOMVO voBOM = (ITM_BOMVO)(arrComponentNotInRouting[i]);
					ITM_Product voSubProduct = new ITM_Product();
					ITM_CostVO voSubCost = new ITM_CostVO();
					// get sub product info
                    voSubProduct = new ProductItemInfoBO().GetProductInfo(voBOM.ComponentID);
					// sub product cost
					voSubCost = (ITM_CostVO) (dsCost.GetCostByProduct(voSubProduct.ProductID));
					// now we calculate item cost base on operation cost
					#region item value added cost

					// 3. VaddCostMachineSetup03 = sum(sub VaddCostMachineSetup03)
					voCost.VAddCostMachineSetup03 += voSubCost.VAddCostMachineSetup03;
					// 4. VaddCostMachineSetupFixed04 = sum(sub VaddCostMachineSetupFixed04)
					voCost.VAddCostMachineSetupFixed04 += voSubCost.VAddCostMachineSetupFixed04;
					// 5. VaddCostMachineSetupVar05 = sum(sub VaddCostMachineSetupVar05)
					voCost.VAddCostMachineSetupVar05 += voSubCost.VAddCostMachineSetupVar05;
					// 6. VaddCostMachineRun06 = sum(sub VaddCostMachineRun06)
					voCost.VAddCostMachineRun06 += voSubCost.VAddCostMachineRun06;
					// 7. VaddCostMachineFixed07 = sum(sub VaddCostMachineFixed07)
					voCost.VAddCostMachineFixed07 += voSubCost.VAddCostMachineFixed07;
					// 8. VaddCostMachineVariable08 = sum(ITM_CostOperation.VaddCostMachineVariable08)
					voCost.VAddCostMachineVariable08 += voSubCost.VAddCostMachineVariable08;
					// 9. VaddCostLaborSetup09 = sum(ITM_CostOperation.VaddCostLaborSetup09)
					voCost.VAddCostLaborSetup09 += voSubCost.VAddCostLaborSetup09;
					// 10. VaddCostLaborSetupFixed10 = sum(ITM_CostOperation.VaddCostLaborSetupFixed10)
					voCost.VAddCostLaborSetupFixed10 += voSubCost.VAddCostLaborSetupFixed10;
					// 11. VaddCostLaborSetupVar11 = sum(ITM_CostOperation.VaddCostLaborSetupVar11)
					voCost.VAddCostLaborSetupVariable11 += voSubCost.VAddCostLaborSetupVariable11;
					// 12. VaddCostLaborRun12 = sum(ITM_CostOperation.VaddCostLaborRun12)
					voCost.VAddCostLaborRun12 += voSubCost.VAddCostLaborRun12;
					// 13. VaddCostLaborFixed13 = sum(ITM_CostOperation.VaddCostLaborFixed13)
					voCost.VAddCostLaborFixed13 += voSubCost.VAddCostLaborFixed13;
					// 14. VaddCostLaborVariable14 = sum(ITM_CostOperation.VaddCostLaborVariable14)
					voCost.VAddCostLaborVariable14 += voSubCost.VAddCostLaborVariable14;
					// total
					voCost.VAddCostTotalAmount21 = voCost.VAddCostMaterial01 + voCost.VAddCostMaterialOverHead02 
						+ voCost.VAddCostMachineSetup03 + voCost.VAddCostMachineSetupFixed04
						+ voCost.VAddCostMachineSetupVar05 + voCost.VAddCostMachineRun06
						+ voCost.VAddCostMachineFixed07 + voCost.VAddCostMachineVariable08
						+ voCost.VAddCostLaborSetup09 + voCost.VAddCostLaborSetupFixed10
						+ voCost.VAddCostLaborSetupVariable11 + voCost.VAddCostLaborRun12
						+ voCost.VAddCostLaborFixed13 + voCost.VAddCostLaborVariable14
						+ voCost.VAddCostOutsideProc15 + voCost.VAddCostAssemblyScrap16
						+ voCost.VAddCostShrink17 + voCost.VAddCostFreight18
						+ voCost.VAddCostUserStandard1_19 + voCost.VAddCostUserStandard2_20;

					#endregion

					#region item components cost

					// 1. ComMaterial01 = sum(sub ComMaterial01)
					voCost.ComMaterial01 += voSubCost.ComMaterial01;
					// 2. ComMaterialOverHead02 = sum(sub ComMaterialOverHead02) 
					voCost.ComMaterialOverHead02 += voSubCost.ComMaterialOverHead02;
					// 3. ComMachineSetup03 =sum(sub ComMachineSetup03)
					voCost.ComMachineSetup03 += voSubCost.ComMachineSetup03;
					// 4. ComMachineSetupFixed04 = sum(sub ComMachineSetupFixed04)
					voCost.ComMachineSetupFixed04 += voSubCost.ComMachineSetupFixed04;
					// 5.ComMachineSetupVar05 = sum(sub ComMachineSetupVar05)
					voCost.ComMachineSetupVar05 += voSubCost.ComMachineSetupVar05;
					// 6.ComMachineRun06 =  sum(sub ComMachineRun06)
					voCost.ComMachineRun06 += voSubCost.ComMachineRun06;
					// 7.ComMachineFixed07 =sum(sub ComMachineFixed07)
					voCost.ComMachineFixed07 += voSubCost.ComMachineFixed07;
					// 8. ComMachineVariable08 =  sum(sub ComMachineVariable08)
					voCost.ComMachineVariable08 += voSubCost.ComMachineVariable08;
					// 9.ComLaborSetup09 =sum(sub ComLaborSetup09)
					voCost.ComLaborSetup09 += voSubCost.ComLaborSetup09;
					// 10.ComLaborSetupFixed10 =sum(sub ComLaborSetupFixed10)
					voCost.ComLaborSetupFixed10 += voSubCost.ComLaborSetupFixed10;
					// 11.ComLaborSetupVariable11= sum(sub ComLaborSetupVariable11)
					voCost.ComLaborSetupVariable11 += voSubCost.ComLaborSetupVariable11;
					// 12.ComLaborRun12=  sum(sub ComLaborRun12)
					voCost.ComLaborRun12 += voSubCost.ComLaborRun12;
					// 13.ComLaborFixed13 =sum (sub ComLaborFixed13)
					voCost.ComLaborFixed13 += voSubCost.ComLaborFixed13;
					// 14.ComLaborVariable14= sum(sub ComLaborVariable14)
					voCost.ComLaborVariable14 += voSubCost.ComLaborVariable14;
					// 15.ComOutsideProc15=  Sum(sub ComOutsideProc15)
					voCost.ComOutsideProc15 += voSubCost.ComOutsideProc15;
					// 16.ComAssemblyScrap16 =sum(sub ComAssemblyScrap16)
					voCost.ComAssemblyScrap16 += voSubCost.ComAssemblyScrap16;
					// 17.ComShrink17 =sum(sub ComShrink17)
					voCost.ComShrink17 += voSubCost.ComShrink17;
					// 18.ComFreight18 = sum(sub ComFreight18)
					voCost.ComFreight18 += voSubCost.ComFreight18;
					// 19.ComUserStandard1_19 = sum(sub ComUserStandard1_19)
					voCost.ComUserStandard1_19 += voSubCost.ComUserStandard1_19;
					// 20.ComUserStandard2_20 = sum(sub ComUserStandard2_20)
					voCost.ComUserStandard2_20 += voSubCost.ComUserStandard2_20;
					// 21.ComTotalAmount21 =sum (Com1 -> Com20)
					voCost.ComTotalAmount21 += voCost.ComMaterial01 + voCost.ComMaterialOverHead02
						+ voCost.ComMachineSetup03 + voCost.ComMachineSetupFixed04
						+ voCost.ComMachineSetupVar05 + voCost.ComMachineRun06
						+ voCost.ComMachineFixed07 + voCost.ComMachineVariable08
						+ voCost.ComLaborSetup09 + voCost.ComLaborSetupFixed10
						+ voCost.ComLaborSetupVariable11 + voCost.ComLaborRun12
						+ voCost.ComLaborFixed13 + voCost.ComLaborVariable14
						+ voCost.ComOutsideProc15 + voCost.ComAssemblyScrap16
						+ voCost.ComShrink17 + voCost.ComFreight18
						+ voCost.ComUserStandard1_19 + voCost.ComUserStandard2_20;

					#endregion
				}

				#endregion

				#region Item cost

				// sum of materials cost and routing cost will be item cost

				voCost.ItemCostMaterial01 = voCost.ComMaterial01 + voCost.VAddCostMaterial01;
				voCost.ItemCostMaterialOverHead02 = voCost.ComMaterialOverHead02 + voCost.VAddCostMaterialOverHead02;
				voCost.ItemCostMachineSetup03 = voCost.ComMachineSetup03 + voCost.VAddCostMachineSetup03;
				voCost.ItemCostMachineSetupFixed04 = voCost.ComMachineSetupFixed04 + voCost.VAddCostMachineSetupFixed04;
				voCost.ItemCostMachineSetupVar05 = voCost.ComMachineSetupVar05 + voCost.VAddCostMachineSetupVar05;
				voCost.ItemCostMachineRun06 = voCost.ComMachineRun06 + voCost.VAddCostMachineRun06;
				voCost.ItemCostMachineFixed07 = voCost.ComMachineFixed07 + voCost.VAddCostMachineFixed07;
				voCost.ItemCostMachineVariable08 = voCost.ComMachineVariable08 + voCost.VAddCostMachineVariable08;
				voCost.ItemCostLaborSetup09 = voCost.ComLaborSetup09 + voCost.VAddCostLaborSetup09;
				voCost.ItemCostLaborSetupFixed10 = voCost.ComLaborSetupFixed10 + voCost.VAddCostLaborSetupFixed10;
				voCost.ItemCostLaborSetupVariable11 = voCost.ComLaborSetupVariable11 + voCost.VAddCostLaborSetupVariable11;
				voCost.ItemCostLaborRun12 = voCost.ComLaborRun12 + voCost.VAddCostLaborRun12;
				voCost.ItemCostLaborFixed13 = voCost.ComLaborFixed13 + voCost.VAddCostLaborFixed13;
				voCost.ItemCostLaborVariable14 = voCost.ComLaborVariable14 + voCost.VAddCostLaborVariable14;
				voCost.ItemCostOutsideProc15 = voCost.ComOutsideProc15 + voCost.VAddCostOutsideProc15;
				voCost.ItemCostAssemblyScrap16 = voCost.ComAssemblyScrap16 + voCost.VAddCostAssemblyScrap16;
				voCost.ItemCostShrink17 = voCost.ComShrink17 + voCost.VAddCostShrink17;
				voCost.ItemCostFreight18 = voCost.ComFreight18 + voCost.VAddCostFreight18;
				voCost.ItemCostUserStandard1_19 = voCost.ComUserStandard1_19 + voCost.VAddCostUserStandard1_19;
				voCost.ItemCostUserStandard2_20 = voCost.ComUserStandard2_20 + voCost.VAddCostUserStandard2_20;
				voCost.ItemCostTotalAmount21 = voCost.ComTotalAmount21 + voCost.VAddCostTotalAmount21;

				#endregion

				// save to database
				dsCost.Add(voCost);
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method uses to roll up cost of all sub product to specified product
		///    </Description>
		///    <Inputs>
		///        int Id, ArrayList AllBomItem
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       N/A
		///    </Returns>
		///    <Authors>
		///       DungLa
		///    </Authors>
		///    <History>
		///       16-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void RollUpCost(int pintCCNID, DateTime pdtmRollUpDate, int pintProductID, ArrayList parrBOMItems)
		{
			try
			{
				// DS
				ITM_CostDS dsCost = new ITM_CostDS();
				ITM_CostCenterRateDS dsCostCenterRate = new ITM_CostCenterRateDS();
				ITM_CostOperationDS dsCostOperation = new ITM_CostOperationDS();
				ITM_ProductDS dsProduct = new ITM_ProductDS();
				ITM_RoutingDS dsRouting = new ITM_RoutingDS();
				// VO
				ITM_Product voProduct = new ITM_Product();
				ITM_CostVO voCost = new ITM_CostVO();
				ITM_CostOperationVO voCostOperation = null;
				ITM_RoutingVO voRouting = new ITM_RoutingVO();
				ITM_CostCenterRateVO voCostCenterRate = new ITM_CostCenterRateVO();

				// get current product info
				voProduct = new ProductItemInfoBO().GetProductInfo(pintProductID);

				// get cost center rate of current product
                int iCostCenterID=voProduct.CostCenterID??0;
                voCostCenterRate = (ITM_CostCenterRateVO)dsCostCenterRate.GetObjectVO(iCostCenterID, pdtmRollUpDate);
				// delete ITM_Cost buy ProductID, CCNID
				dsCost.Delete(pintCCNID, pintProductID);
				// delete ITM_CostOperation ProductID, CCNID
				dsCostOperation.Delete(pintCCNID, pintProductID);
				// get list routing of current product
				ArrayList arrRoutings = dsRouting.GetObjectVOs(pintProductID);
				arrRoutings.TrimToSize();
				// create new Cost, CostOperation object
				voCost.ProductID = pintProductID;
				voCost.CCNID = pintCCNID;
				voCost.SingleRollUpDate = pdtmRollUpDate;

				#region Rollup components cost 

				// cost for each operation first
				for (int j = 0; j < arrRoutings.Count; j++)
				{
					voRouting = (ITM_RoutingVO)arrRoutings[j];
					voCostOperation = new ITM_CostOperationVO();
					// routing machine cost center
					ITM_CostCenterRateVO voMachineCostCenterRate = (ITM_CostCenterRateVO)dsCostCenterRate.GetObjectVO(voRouting.MachineCostCenterID, pdtmRollUpDate);
					if (voMachineCostCenterRate == null)
						voMachineCostCenterRate = new ITM_CostCenterRateVO();
					// routing labor cost center
					ITM_CostCenterRateVO voLaborCostCenterRate = (ITM_CostCenterRateVO)dsCostCenterRate.GetObjectVO(voRouting.LaborCostCenterID, pdtmRollUpDate);
					if (voLaborCostCenterRate == null)
						voLaborCostCenterRate = new ITM_CostCenterRateVO();
					// general information
					voCostOperation.CCNID = pintCCNID;
					voCostOperation.ProductID = pintProductID;
					voCostOperation.RoutingID = voRouting.RoutingID;
					
					#region operation value added cost

					if (voRouting.SetupQuantity > 0)
					{
						// 3. VaddCostMachineSetup03=((ITM_Routing.MachineSetupTime * ITM_CostCenterRate.MachineSetup)/ITM_Routing.SetupQuantity)
						voCostOperation.VAddCostMachineSetup03 = decimal.Round((voRouting.MachineSetupTime * voMachineCostCenterRate.MachineSetup) / voRouting.SetupQuantity, DECIMAL_ROUND_NUMBER);
						// 4. VaddCostMachineSetupFixed04 = (ITM_Routing.MachineSetupTime * ITM_CostCenterRate.MachineSetupFixedOverhead/ITM_Routing.SetupQuantity)
						voCostOperation.VAddCostMachineSetupFixed04 = decimal.Round((voRouting.MachineSetupTime * voMachineCostCenterRate.MachineSetupFixedOverhead) / voRouting.SetupQuantity, DECIMAL_ROUND_NUMBER);
						// 9. VaddCostLaborSetup09=((ITM_Routing.LaborSetupTime * ITM_CostCenterRate.laborSetup)/ITM_Routing.SetupQuantity)
						voCostOperation.VAddCostLaborSetup09 = decimal.Round((voRouting.LaborSetupTime * voLaborCostCenterRate.LaborSetup) / voRouting.SetupQuantity, DECIMAL_ROUND_NUMBER);
						// 10. VaddCostLaborSetupFixed10 = (ITM_Routing.LaborSetupTime * ITM_CostCenterRate.LaborSetupFixedOverhead / ITM_Routing.SetupQuantity)
						voCostOperation.VAddCostLaborSetupFixed10 = decimal.Round((voRouting.LaborSetupTime * voLaborCostCenterRate.LaborSetupFixedOverhead) / voRouting.SetupQuantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ValueAddedLaborSetupHour = decimal.Round(voRouting.LaborSetupTime / voRouting.SetupQuantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ValueAddedMachineSetupHour = decimal.Round(voRouting.MachineSetupTime / voRouting.SetupQuantity, DECIMAL_ROUND_NUMBER);
					}
					// 5. VaddCostMachineSetupVar05 = (VaddCostMachineSetup03 * ITM_CostCenterRate.MachineSetupVariableOverhead %)
					voCostOperation.VAddCostMachineSetupVar05 = decimal.Round(voCostOperation.VAddCostMachineSetup03 * voMachineCostCenterRate.MachineSetupVariableOverhead / 100, DECIMAL_ROUND_NUMBER);
					if (voRouting.RunQuantity > 0)
					{
						// 6. VaddCostMachineRun06=((ITM_Routing.MachineRunTime * ITM_CostCenterRate.MachineRunRate) / ITM_Routing.RunQuantity)
						voCostOperation.VAddCostMachineRun06 = decimal.Round((voRouting.MachineRunTime * voMachineCostCenterRate.MachineRun) / voRouting.RunQuantity, DECIMAL_ROUND_NUMBER);
						// 7. VaddCostMachineFixed07 = (ITM_Routing.MachineRunTime * ITM_CostCenterRate.MachineRunFixedOverhead/ITM_Routing.RunQuantity)
						voCostOperation.VAddCostMachineFixed07 = decimal.Round((voRouting.MachineRunTime * voMachineCostCenterRate.MachineRunFixedOverhead) / voRouting.RunQuantity, DECIMAL_ROUND_NUMBER);
						// 12. VaddCostLaborRun12=((ITM_Routing.LaborRunTime * ITM_CostCenterRate.LaborRun)/ITM_Routing.RunQuantity)
						voCostOperation.VAddCostLaborRun12 = decimal.Round((voRouting.LaborRunTime * voLaborCostCenterRate.LaborRun) / voRouting.RunQuantity, DECIMAL_ROUND_NUMBER);
						// 13. VaddCostLaborFixed13 = (ITM_Routing.CrewSize * ITM_CostCenterRate.LaborRunFixedOverhead/ITM_Routing.RunQuantity)
						voCostOperation.VAddCostLaborFixed13 = decimal.Round((voRouting.LaborRunTime * voLaborCostCenterRate.LaborRunFixedOverhead) / voRouting.RunQuantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ValueAddedLaborRunHour = decimal.Round(voRouting.LaborRunTime / voRouting.RunQuantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ValueAddedMachineRunHour = decimal.Round(voRouting.MachineRunTime/ voRouting.RunQuantity, DECIMAL_ROUND_NUMBER);
					}
					// 8. VaddCostMachineVariable08 = (VaddCostMachineRun06 * ITM_CostCenterRate.MachineRunVariableOverhead / 100)
					voCostOperation.VAddCostMachineVariable08 = decimal.Round(voCostOperation.VAddCostMachineRun06 * voMachineCostCenterRate.MachineRunVariableOverhead / 100, DECIMAL_ROUND_NUMBER);
					// 11. VaddCostLaborSetupVar11 = (VaddCostLaborSetup09 * ITM_CostCenterRate.LaborSetupVariableOverhead %)
					voCostOperation.VAddCostLaborSetupVariable11 = decimal.Round(voCostOperation.VAddCostLaborSetup09 * voLaborCostCenterRate.LaborSetupVariableOverhead / 100, DECIMAL_ROUND_NUMBER);
					// 14. VaddCostLaborVariable14 = (VaddCostLaborRun12 * ITM_CostCenterRate.LaborRunVariableOverhead %)
					voCostOperation.VAddCostLaborVariable14 = decimal.Round(voCostOperation.VAddCostLaborRun12 * voLaborCostCenterRate.LaborRunVariableOverhead / 100, DECIMAL_ROUND_NUMBER);
					// 15. VaddOutsiteProc cost
					voCostOperation.VAddCostOutsideProc15 = voRouting.OSCost;
					// total
					voCostOperation.VAddCostTotalAmount21 = decimal.Round(voCostOperation.VAddCostMaterial01 + voCostOperation.VAddCostMaterialOverHead02 
						+ voCostOperation.VAddCostMachineSetup03 + voCostOperation.VAddCostMachineSetupFixed04
						+ voCostOperation.VAddCostMachineSetupVar05 + voCostOperation.VAddCostMachineRun06
						+ voCostOperation.VAddCostMachineFixed07 + voCostOperation.VAddCostMachineVariable08
						+ voCostOperation.VAddCostLaborSetup09 + voCostOperation.VAddCostLaborSetupFixed10
						+ voCostOperation.VAddCostLaborSetupVariable11 + voCostOperation.VAddCostLaborRun12
						+ voCostOperation.VAddCostLaborFixed13 + voCostOperation.VAddCostLaborVariable14
						+ voCostOperation.VAddCostOutsideProc15 + voCostOperation.VAddCostAssemblyScrap16
						+ voCostOperation.VAddCostShrink17 + voCostOperation.VAddCostFreight18
						+ voCostOperation.VAddCostUserStandard1_19 + voCostOperation.VAddCostUserStandard2_20, DECIMAL_ROUND_NUMBER);

					#endregion

					#region operation components cost

					for (int i = 0; i < parrBOMItems.Count; i++)
					{
						// BOM item
						ITM_BOMVO voBOM = (ITM_BOMVO)(parrBOMItems[i]);
						// check if current product have component(s) or not
						ArrayList arrAllSubProducts = new ArrayList();
						arrAllSubProducts = GetAllItemBom(voBOM.ComponentID);
						arrAllSubProducts.TrimToSize();
						// if component have sub component then recursive
						if (arrAllSubProducts.Count > 0)
						{
							RollUpCost(pintCCNID, pdtmRollUpDate, voBOM.ComponentID, arrAllSubProducts);
						}
						// if current component is not in the list of routing
						// then continue to next component
						if (voBOM.RoutingID != voRouting.RoutingID)
						{
							continue;
						}
						ITM_Product voSubProduct = new ITM_Product();
						ITM_CostVO voSubCost = new ITM_CostVO();
						// get sub product info
                        voSubProduct = new ProductItemInfoBO().GetProductInfo(voBOM.ComponentID);
						// sub product cost
						voSubCost = (ITM_CostVO) (dsCost.GetCostByProduct(voSubProduct.ProductID));
						
						// 1. ComMaterial01 = sum(sub  ComMaterial01 * voBOM.Quantity)
						voCostOperation.ComMaterial01 += decimal.Round(voSubCost.ItemCostMaterial01 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 2. ComMaterialOverHead02 = sum( voCostOperation.ComMaterial01 * MaterialOH from CostCenterRate / 100) 
						voCostOperation.ComMaterialOverHead02 += decimal.Round((voSubCost.ItemCostMaterial01 * voBOM.Quantity * voCostCenterRate.MaterialOverhead) / 100, DECIMAL_ROUND_NUMBER);
						// 3. ComMachineSetup03 =sum((ITM_Routing.Machine Setup Time * ITM_CostCenterRate.Machine Setup Rate) / ITM_Routing.SetupQuantity)
						voCostOperation.ComMachineSetup03 += decimal.Round(voSubCost.ItemCostMachineSetup03 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 4. ComMachineSetupFixed04 = sum((ITM_Routing.Machine Setup Time * MachineSetupFixedOverhead ) / ITM_Routing.SetupQuantity)
						voCostOperation.ComMachineSetupFixed04 += decimal.Round(voSubCost.ItemCostMachineSetupFixed04 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 5.ComMachineSetupVar05 = sum(sub ComMachineSetup03  *ITM_CostCenterRate. Machine Setup Var Overhead * voBOM.Quantity)
						voCostOperation.ComMachineSetupVar05 += decimal.Round(voSubCost.ItemCostMachineSetupVar05 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 6.ComMachineRun06 =  sum((ITM_Routing.Machine Run Time * ITM_CostCenterRate.MachineRun) / ITM_Routing.RunQuantity)
						voCostOperation.ComMachineRun06 += decimal.Round(voSubCost.ItemCostMachineRun06 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 7.ComMachineFixed07 =sum(ITM_Routing.Machine Run Time * ITM_CostCenterRate.MachineRunFixedOverHead ) /  ITM_Routing.RunQuantity
						voCostOperation.ComMachineFixed07 += decimal.Round(voSubCost.ItemCostMachineFixed07 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 8. ComMachineVariable08 =  sum(sub ComMachineRun06*ITM_CostCenterRate. Machine Run Var Overhead * voBOM.Quantity)
						voCostOperation.ComMachineVariable08 += decimal.Round(voSubCost.ItemCostMachineVariable08 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 9.ComLaborSetup09 =sum((ITM_Routing.Labor Setup Time * ITM_CostCenterRate.Labor Setup) / ITM_Routing.Setup Quantity)
						voCostOperation.ComLaborSetup09 += decimal.Round(voSubCost.ItemCostLaborSetup09 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 10.ComLaborSetupFixed10 =sum((ITM_Routing.Labor Setup Time * ITM_CostCenterRate.LaborSetupFixedOverHead) / ITM_Routing.Setup Quantity)
						voCostOperation.ComLaborSetupFixed10 += decimal.Round(voSubCost.ItemCostLaborSetupFixed10 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 11.ComLaborSetupVariable11= sum(sub ComLaborSetup09*ITM_CostCenterRate. Labor Setup Var Overhead * voBOM.Quantity)
						voCostOperation.ComLaborSetupVariable11 += decimal.Round(voSubCost.ItemCostLaborSetupVariable11 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 12.ComLaborRun12=  sum((ITM_Routing.Labor Run Time * ITM_CostCenterRate.LaborRun) / ITM_Routing.RunQuantity)
						voCostOperation.ComLaborRun12 += voSubCost.ItemCostLaborRun12 * voBOM.Quantity;
						// 13.ComLaborFixed13 =sum ((ITM_Routing.Labor Run Time * ITM_CostCenterRate.LaborRunFixedOverhead ) /  ITM_Routing.RunQuantity)
						voCostOperation.ComLaborFixed13 += decimal.Round(voSubCost.ItemCostLaborFixed13 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 14.ComLaborVariable14= sum(sub ComLaborRun12*ITM_CostCenterRate.LaborRunVarOverhead * voBOM.Quantity)
						voCostOperation.ComLaborVariable14 += decimal.Round(voSubCost.ItemCostLaborVariable14 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 15.ComOutsideProc15=  Sum(sub ComOutsideProc15 * ITM_BOM.Quantity)
						voCostOperation.ComOutsideProc15 += decimal.Round(voSubCost.ItemCostOutsideProc15 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 16.ComAssemblyScrap16 =sum(sub ITM_Product.ScrapPercent * ITM_BOM.Quantity * ITM_Cost.ComTotalAmount)
						voCostOperation.ComAssemblyScrap16 += decimal.Round(voSubCost.ItemCostAssemblyScrap16 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 17.ComShrink17 =sum(ITM_Bom.Shrink* ITM_BOM.Quantity*ITM_Cost.ComTotalAmount)
						voCostOperation.ComShrink17 += decimal.Round(voBOM.Quantity * voSubCost.ItemCostShrink17, DECIMAL_ROUND_NUMBER);
						// 18.ComFreight18 = sum(sub ComFreight18 * ITM_BOM.Quantity)
						voCostOperation.ComFreight18 += decimal.Round(voSubCost.ItemCostFreight18 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 19.ComUserStandard1_19 = sum(sub ComUserStandard1_19 * ITM_BOM.Quantity)
						voCostOperation.ComUserStandard1_19 += decimal.Round(voSubCost.ItemCostUserStandard1_19 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						// 20.ComUserStandard2_20 = sum(sub ComUserStandard2_20 * ITM_BOM.Quantity)
						voCostOperation.ComUserStandard2_20 += decimal.Round(voSubCost.ItemCostUserStandard2_20 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ComponentLaborRunHour += decimal.Round(voSubCost.ItemLaborRunHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ComponentLaborSetupHour += decimal.Round(voSubCost.ItemLaborSetupHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ComponentMachineRunHour += decimal.Round(voSubCost.ItemMachineRunHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
						voCostOperation.ComponentMachineSetupHour += decimal.Round(voSubCost.ItemMachineSetupHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					}
					// 21.ComTotalAmount21 =sum (Com1 -> Com20)
					voCostOperation.ComTotalAmount21 += decimal.Round(voCostOperation.ComMaterial01 + voCostOperation.ComMaterialOverHead02 
						+ voCostOperation.ComMachineSetup03 + voCostOperation.ComMachineSetupFixed04 + voCostOperation.ComMachineSetupVar05 
						+ voCostOperation.ComMachineRun06 + voCostOperation.ComMachineFixed07 + voCostOperation.ComMachineVariable08 
						+ voCostOperation.ComLaborSetup09 + voCostOperation.ComLaborSetupFixed10 + voCostOperation.ComLaborSetupVariable11 
						+ voCostOperation.ComLaborRun12 + voCostOperation.ComLaborFixed13 + voCostOperation.ComLaborVariable14 
						+ voCostOperation.ComOutsideProc15 + voCostOperation.ComAssemblyScrap16 + voCostOperation.ComShrink17 
						+ voCostOperation.ComFreight18 + voCostOperation.ComUserStandard1_19 + voCostOperation.ComUserStandard2_20, DECIMAL_ROUND_NUMBER);

					#endregion

					#region routing operation cost

					voCostOperation.OperCostMaterial01 = decimal.Round(voCostOperation.ComMaterial01 + voCostOperation.VAddCostMaterial01, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMaterialOverHead02 = decimal.Round(voCostOperation.ComMaterialOverHead02 + voCostOperation.VAddCostMaterialOverHead02, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMaterialOverHead02 = decimal.Round(voCostOperation.ComMaterialOverHead02 + voCostOperation.VAddCostMaterialOverHead02, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMachineSetup03 = decimal.Round(voCostOperation.ComMachineSetup03 + voCostOperation.VAddCostMachineSetup03, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMachineSetupFixed04 = decimal.Round(voCostOperation.ComMachineSetupFixed04 + voCostOperation.VAddCostMachineSetupFixed04, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMachineSetupVar05 = decimal.Round(voCostOperation.ComMachineSetupVar05 + voCostOperation.VAddCostMachineSetupVar05, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMachineRun06 = decimal.Round(voCostOperation.ComMachineRun06 + voCostOperation.VAddCostMachineRun06, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMachineFixed07 = decimal.Round(voCostOperation.ComMachineFixed07 + voCostOperation.VAddCostMachineFixed07, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostMachineVariable08 = decimal.Round(voCostOperation.ComMachineVariable08 + voCostOperation.VAddCostMachineVariable08, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostLaborSetup09 = decimal.Round(voCostOperation.ComLaborSetup09 + voCostOperation.VAddCostLaborSetup09, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostLaborSetupFixed10 = decimal.Round(voCostOperation.ComLaborSetupFixed10 + voCostOperation.VAddCostLaborSetupFixed10, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostLaborSetupVariable11 = decimal.Round(voCostOperation.ComLaborSetupVariable11 + voCostOperation.VAddCostLaborSetupVariable11, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostLaborRun12 = decimal.Round(voCostOperation.ComLaborRun12 + voCostOperation.VAddCostLaborRun12, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostLaborFixed13 = decimal.Round(voCostOperation.ComLaborFixed13 + voCostOperation.VAddCostLaborFixed13, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostLaborVariable14 = decimal.Round(voCostOperation.ComLaborVariable14 + voCostOperation.VAddCostLaborVariable14, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostOutsideProc15 = decimal.Round(voCostOperation.ComOutsideProc15 + voCostOperation.VAddCostOutsideProc15, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostAssemblyScrap16 = decimal.Round(voCostOperation.ComAssemblyScrap16 + voCostOperation.VAddCostAssemblyScrap16, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostShrink17 = decimal.Round(voCostOperation.ComShrink17 + voCostOperation.VAddCostShrink17, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostFreight18 = decimal.Round(voCostOperation.ComFreight18 + voCostOperation.VAddCostFreight18, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostUserStandard1_19 = decimal.Round(voCostOperation.ComUserStandard1_19 + voCostOperation.VAddCostUserStandard1_19, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostUserStandard2_20 = decimal.Round(voCostOperation.ComUserStandard2_20 + voCostOperation.VAddCostUserStandard2_20, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperCostTotalAmount21 = decimal.Round(voCostOperation.ComTotalAmount21 + voCostOperation.VAddCostTotalAmount21, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperationLaborRunHour = decimal.Round(voCostOperation.ComponentLaborRunHour + voCostOperation.ValueAddedLaborRunHour, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperationLaborSetupHour = decimal.Round(voCostOperation.ComponentLaborSetupHour + voCostOperation.ValueAddedLaborSetupHour, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperationMachineRunHour = decimal.Round(voCostOperation.ComponentMachineRunHour + voCostOperation.ValueAddedMachineRunHour, DECIMAL_ROUND_NUMBER);
					voCostOperation.OperationMachineSetupHour = decimal.Round(voCostOperation.ComponentMachineSetupHour + voCostOperation.ValueAddedMachineSetupHour, DECIMAL_ROUND_NUMBER);

					#endregion

					// save to database
					voCostOperation.CostOperationID = dsCostOperation.AddAndReturnID(voCostOperation);

					// now we calculate item cost base on operation cost
					#region item value added cost

					// 3. VaddCostMachineSetup03 = sum(ITM_CostOperation.VaddCostMachineSetup03)
					voCost.VAddCostMachineSetup03 += decimal.Round(voCostOperation.VAddCostMachineSetup03, DECIMAL_ROUND_NUMBER);
					// 4. VaddCostMachineSetupFixed04 = sum(ITM_CostOperation.VaddCostMachineSetupFixed04)
					voCost.VAddCostMachineSetupFixed04 += decimal.Round(voCostOperation.VAddCostMachineSetupFixed04, DECIMAL_ROUND_NUMBER);
					// 5. VaddCostMachineSetupVar05 = sum(ITM_CostOperation.VaddCostMachineSetupVar05)
					voCost.VAddCostMachineSetupVar05 += decimal.Round(voCostOperation.VAddCostMachineSetupVar05, DECIMAL_ROUND_NUMBER);
					// 6. VaddCostMachineRun06 = sum(ITM_CostOperation.VaddCostMachineRun06)
					voCost.VAddCostMachineRun06 += decimal.Round(voCostOperation.VAddCostMachineRun06, DECIMAL_ROUND_NUMBER);
					// 7. VaddCostMachineFixed07 = sum(ITM_CostOperation.VaddCostMachineFixed07)
					voCost.VAddCostMachineFixed07 += decimal.Round(voCostOperation.VAddCostMachineFixed07, DECIMAL_ROUND_NUMBER);
					// 8. VaddCostMachineVariable08 = sum(ITM_CostOperation.VaddCostMachineVariable08)
					voCost.VAddCostMachineVariable08 += decimal.Round(voCostOperation.VAddCostMachineVariable08, DECIMAL_ROUND_NUMBER);
					// 9. VaddCostLaborSetup09 = sum(ITM_CostOperation.VaddCostLaborSetup09)
					voCost.VAddCostLaborSetup09 += decimal.Round(voCostOperation.VAddCostLaborSetup09, DECIMAL_ROUND_NUMBER);
					// 10. VaddCostLaborSetupFixed10 = sum(ITM_CostOperation.VaddCostLaborSetupFixed10)
					voCost.VAddCostLaborSetupFixed10 += decimal.Round(voCostOperation.VAddCostLaborSetupFixed10, DECIMAL_ROUND_NUMBER);
					// 11. VaddCostLaborSetupVar11 = sum(ITM_CostOperation.VaddCostLaborSetupVar11)
					voCost.VAddCostLaborSetupVariable11 += decimal.Round(voCostOperation.VAddCostLaborSetupVariable11, DECIMAL_ROUND_NUMBER);
					// 12. VaddCostLaborRun12 = sum(ITM_CostOperation.VaddCostLaborRun12)
					voCost.VAddCostLaborRun12 += decimal.Round(voCostOperation.VAddCostLaborRun12, DECIMAL_ROUND_NUMBER);
					// 13. VaddCostLaborFixed13 = sum(ITM_CostOperation.VaddCostLaborFixed13)
					voCost.VAddCostLaborFixed13 += decimal.Round(voCostOperation.VAddCostLaborFixed13, DECIMAL_ROUND_NUMBER);
					// 14. VaddCostLaborVariable14 = sum(ITM_CostOperation.VaddCostLaborVariable14)
					voCost.VAddCostLaborVariable14 += decimal.Round(voCostOperation.VAddCostLaborVariable14, DECIMAL_ROUND_NUMBER);
					// 15. voCost.VAddCostOutsideProc15 += voCostOperation.VAddCostOutsideProc15;
					voCost.VAddCostOutsideProc15 += decimal.Round(voCostOperation.VAddCostOutsideProc15, DECIMAL_ROUND_NUMBER);
					voCost.ValueAddedLaborRunHour += decimal.Round(voCostOperation.ValueAddedLaborRunHour, DECIMAL_ROUND_NUMBER);
					voCost.ValueAddedLaborSetupHour += decimal.Round(voCostOperation.ValueAddedLaborSetupHour, DECIMAL_ROUND_NUMBER);
					voCost.ValueAddedMachineRunHour += decimal.Round(voCostOperation.ValueAddedMachineRunHour, DECIMAL_ROUND_NUMBER);
					voCost.ValueAddedMachineSetupHour += decimal.Round(voCostOperation.ValueAddedMachineSetupHour, DECIMAL_ROUND_NUMBER);
					// total
					voCost.VAddCostTotalAmount21 = decimal.Round(voCost.VAddCostMaterial01 + voCost.VAddCostMaterialOverHead02 
						+ voCost.VAddCostMachineSetup03 + voCost.VAddCostMachineSetupFixed04
						+ voCost.VAddCostMachineSetupVar05 + voCost.VAddCostMachineRun06
						+ voCost.VAddCostMachineFixed07 + voCost.VAddCostMachineVariable08
						+ voCost.VAddCostLaborSetup09 + voCost.VAddCostLaborSetupFixed10
						+ voCost.VAddCostLaborSetupVariable11 + voCost.VAddCostLaborRun12
						+ voCost.VAddCostLaborFixed13 + voCost.VAddCostLaborVariable14
						+ voCost.VAddCostOutsideProc15 + voCost.VAddCostAssemblyScrap16
						+ voCost.VAddCostShrink17 + voCost.VAddCostFreight18
						+ voCost.VAddCostUserStandard1_19 + voCost.VAddCostUserStandard2_20, DECIMAL_ROUND_NUMBER);

					#endregion

				}

				#endregion

				#region roll up item component cost

				// for all item that does not have routing, roll up will its childs' cost
				for (int i = 0; i < parrBOMItems.Count; i++)
				{
					// BOM item
					ITM_BOMVO voBOM = (ITM_BOMVO)(parrBOMItems[i]);
					// Added: 06 - Oct 2005 by DUNGLA
					// check if current product have component(s) or not
					ArrayList arrAllSubProducts = new ArrayList();
					arrAllSubProducts = GetAllItemBom(voBOM.ComponentID);
					arrAllSubProducts.TrimToSize();
					// if component have sub component then recursive
					if (arrAllSubProducts.Count > 0)
					{
						RollUpCost(pintCCNID, pdtmRollUpDate, voBOM.ComponentID, arrAllSubProducts);
					}
					// End of Added 06 - Oct 2005
					ITM_Product voSubProduct = new ITM_Product();
					ITM_CostVO voSubCost = new ITM_CostVO();
					// get sub product info
                    voSubProduct = new ProductItemInfoBO().GetProductInfo(voBOM.ComponentID);
					// sub product cost
					voSubCost = (ITM_CostVO) (dsCost.GetCostByProduct(voSubProduct.ProductID));
					// now we calculate item cost base on operation cost
					
					#region item components cost

					// 1. ComMaterial01 = sum(sub ComMaterial * ITM_Bom.Quantity)
					voCost.ComMaterial01 += decimal.Round(voSubCost.ItemCostMaterial01 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 2. ComMaterialOverHead02 = sum(voCost.ComMaterial01 * voCostCenterRate.MaterialOverhead / 100) 
					voCost.ComMaterialOverHead02 += decimal.Round(voSubCost.ItemCostMaterial01 * voBOM.Quantity * voCostCenterRate.MaterialOverhead / 100, DECIMAL_ROUND_NUMBER);
					// 3. ComMachineSetup03 =sum(sub ComMachineSetup03 * ITM_Bom.Quantity)
					voCost.ComMachineSetup03 += decimal.Round(voSubCost.ItemCostMachineSetup03 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 4. ComMachineSetupFixed04 = sum(sub ComMachineSetupFixed04  * ITM_Bom.Quantity)
					voCost.ComMachineSetupFixed04 += decimal.Round(voSubCost.ItemCostMachineSetupFixed04 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 5.ComMachineSetupVar05 = sum(sub ComMachineSetupVar05 * ITM_Bom.Quantity)
					voCost.ComMachineSetupVar05 += decimal.Round(voSubCost.ItemCostMachineSetupVar05 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 6.ComMachineRun06 =  sum(sub ComMachineRun06 * ITM_Bom.Quantity)
					voCost.ComMachineRun06 += decimal.Round(voSubCost.ItemCostMachineRun06 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 7.ComMachineFixed07 =sum(sub ComMachineFixed07 * ITM_Bom.Quantity)
					voCost.ComMachineFixed07 += decimal.Round(voSubCost.ItemCostMachineFixed07 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 8. ComMachineVariable08 =  sum(sub ComMachineVariable08 * ITM_Bom.Quantity)
					voCost.ComMachineVariable08 += decimal.Round(voSubCost.ItemCostMachineVariable08 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 9.ComLaborSetup09 =sum(sub ComLaborSetup09 * ITM_Bom.Quantity)
					voCost.ComLaborSetup09 += decimal.Round(voSubCost.ItemCostLaborSetup09 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 10.ComLaborSetupFixed10 =sum(sub ComLaborSetupFixed10 * ITM_Bom.Quantity)
					voCost.ComLaborSetupFixed10 += decimal.Round(voSubCost.ItemCostLaborSetupFixed10 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 11.ComLaborSetupVariable11= sum(sub ComLaborSetupVariable11 * ITM_Bom.Quantity)
					voCost.ComLaborSetupVariable11 += decimal.Round(voSubCost.ItemCostLaborSetupVariable11 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 12.ComLaborRun12=  sum(sub ComLaborRun12 * ITM_Bom.Quantity)
					voCost.ComLaborRun12 += decimal.Round(voSubCost.ItemCostLaborRun12 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 13.ComLaborFixed13 =sum (sub ComLaborFixed13 * ITM_Bom.Quantity)
					voCost.ComLaborFixed13 += decimal.Round(voSubCost.ItemCostLaborFixed13 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 14.ComLaborVariable14= sum(sub ComLaborVariable14 * voBOM.Quantity)
					voCost.ComLaborVariable14 += decimal.Round(voSubCost.ItemCostLaborVariable14 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 15.ComOutsideProc15=  Sum(sub ComOutsideProc15 * voBOM.Quantity)
					voCost.ComOutsideProc15 += decimal.Round(voSubCost.ItemCostOutsideProc15 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 16.ComAssemblyScrap16 =sum(sub ComAssemblyScrap16 * voBOM.Quantity)
					voCost.ComAssemblyScrap16 += decimal.Round(voSubCost.ItemCostAssemblyScrap16 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 17.ComShrink17 =sum(sub ComShrink17 * voBOM.Quantity)
					voCost.ComShrink17 += decimal.Round(voSubCost.ItemCostShrink17 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 18.ComFreight18 = sum(sub ComFreight18 * voBOM.Quantity)
					voCost.ComFreight18 += decimal.Round(voSubCost.ItemCostFreight18 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 19.ComUserStandard1_19 = sum(sub ComUserStandard1_19 * voBOM.Quantity)
					voCost.ComUserStandard1_19 += decimal.Round(voSubCost.ItemCostUserStandard1_19 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					// 20.ComUserStandard2_20 = sum(sub ComUserStandard2_20 * voBOM.Quantity)
					voCost.ComUserStandard2_20 += decimal.Round(voSubCost.ItemCostUserStandard2_20 * voBOM.Quantity, DECIMAL_ROUND_NUMBER);

					voCost.ComponentLaborRunHour += decimal.Round(voSubCost.ItemLaborRunHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					voCost.ComponentLaborSetupHour += decimal.Round(voSubCost.ItemLaborSetupHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					voCost.ComponentMachineRunHour += decimal.Round(voSubCost.ItemMachineRunHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					voCost.ComponentMachineSetupHour += decimal.Round(voSubCost.ItemMachineSetupHour * voBOM.Quantity, DECIMAL_ROUND_NUMBER);
					
					#endregion
				}

				// 21.ComTotalAmount21 =sum (Com1 -> Com20)
				voCost.ComTotalAmount21 += decimal.Round(voCost.ComMaterial01 + voCost.ComMaterialOverHead02
					+ voCost.ComMachineSetup03 + voCost.ComMachineSetupFixed04
					+ voCost.ComMachineSetupVar05 + voCost.ComMachineRun06
					+ voCost.ComMachineFixed07 + voCost.ComMachineVariable08
					+ voCost.ComLaborSetup09 + voCost.ComLaborSetupFixed10
					+ voCost.ComLaborSetupVariable11 + voCost.ComLaborRun12
					+ voCost.ComLaborFixed13 + voCost.ComLaborVariable14
					+ voCost.ComOutsideProc15 + voCost.ComAssemblyScrap16
					+ voCost.ComShrink17 + voCost.ComFreight18
					+ voCost.ComUserStandard1_19 + voCost.ComUserStandard2_20, DECIMAL_ROUND_NUMBER);

				#endregion

				#region Item cost

				// sum of materials cost and routing cost will be item cost

				voCost.ItemCostMaterial01 = decimal.Round(voCost.ComMaterial01 + voCost.VAddCostMaterial01, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMaterialOverHead02 = decimal.Round(voCost.ComMaterialOverHead02 + voCost.VAddCostMaterialOverHead02, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMachineSetup03 = decimal.Round(voCost.ComMachineSetup03 + voCost.VAddCostMachineSetup03, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMachineSetupFixed04 = decimal.Round(voCost.ComMachineSetupFixed04 + voCost.VAddCostMachineSetupFixed04, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMachineSetupVar05 = decimal.Round(voCost.ComMachineSetupVar05 + voCost.VAddCostMachineSetupVar05, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMachineRun06 = decimal.Round(voCost.ComMachineRun06 + voCost.VAddCostMachineRun06, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMachineFixed07 = decimal.Round(voCost.ComMachineFixed07 + voCost.VAddCostMachineFixed07, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostMachineVariable08 = decimal.Round(voCost.ComMachineVariable08 + voCost.VAddCostMachineVariable08, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostLaborSetup09 = decimal.Round(voCost.ComLaborSetup09 + voCost.VAddCostLaborSetup09, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostLaborSetupFixed10 = decimal.Round(voCost.ComLaborSetupFixed10 + voCost.VAddCostLaborSetupFixed10, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostLaborSetupVariable11 = decimal.Round(voCost.ComLaborSetupVariable11 + voCost.VAddCostLaborSetupVariable11, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostLaborRun12 = decimal.Round(voCost.ComLaborRun12 + voCost.VAddCostLaborRun12, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostLaborFixed13 = decimal.Round(voCost.ComLaborFixed13 + voCost.VAddCostLaborFixed13, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostLaborVariable14 = decimal.Round(voCost.ComLaborVariable14 + voCost.VAddCostLaborVariable14, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostOutsideProc15 = decimal.Round(voCost.ComOutsideProc15 + voCost.VAddCostOutsideProc15, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostAssemblyScrap16 = decimal.Round(voCost.ComAssemblyScrap16 + voCost.VAddCostAssemblyScrap16, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostShrink17 = decimal.Round(voCost.ComShrink17 + voCost.VAddCostShrink17, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostFreight18 = decimal.Round(voCost.ComFreight18 + voCost.VAddCostFreight18, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostUserStandard1_19 = decimal.Round(voCost.ComUserStandard1_19 + voCost.VAddCostUserStandard1_19, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostUserStandard2_20 = decimal.Round(voCost.ComUserStandard2_20 + voCost.VAddCostUserStandard2_20, DECIMAL_ROUND_NUMBER);
				voCost.ItemCostTotalAmount21 = decimal.Round(voCost.ComTotalAmount21 + voCost.VAddCostTotalAmount21, DECIMAL_ROUND_NUMBER);

				voCost.ItemLaborRunHour = decimal.Round(voCost.ComponentLaborRunHour + voCost.ValueAddedLaborRunHour, DECIMAL_ROUND_NUMBER);
				voCost.ItemLaborSetupHour = decimal.Round(voCost.ComponentLaborSetupHour + voCost.ValueAddedLaborSetupHour, DECIMAL_ROUND_NUMBER);
				voCost.ItemMachineRunHour = decimal.Round(voCost.ComponentMachineRunHour + voCost.ValueAddedMachineRunHour, DECIMAL_ROUND_NUMBER);
				voCost.ItemMachineSetupHour = decimal.Round(voCost.ComponentMachineSetupHour + voCost.ValueAddedMachineSetupHour, DECIMAL_ROUND_NUMBER);

				#endregion

				// save to database
				dsCost.Add(voCost);
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
